Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
55.56% covered (warning)
55.56%
10 / 18
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
InMemoryFilesystemAdapter
55.56% covered (warning)
55.56%
10 / 18
50.00% covered (danger)
50.00%
3 / 6
21.62
0.00% covered (danger)
0.00%
0 / 1
 getNormalizer
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 visibility
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 lastModified
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 copy
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 write
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 createDirectories
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BrianHenryIE\Strauss\Helpers;
4
5use Elazar\Flystream\StripProtocolPathNormalizer;
6use League\Flysystem\Config;
7use League\Flysystem\FileAttributes;
8use League\Flysystem\InMemory\InMemoryFilesystemAdapter as LeagueInMemoryFilesystemAdapter;
9use League\Flysystem\PathNormalizer;
10use League\Flysystem\WhitespacePathNormalizer;
11
12class InMemoryFilesystemAdapter extends LeagueInMemoryFilesystemAdapter implements FlysystemBackCompatTraitInterface
13{
14    use FlysystemBackCompatTrait;
15
16    protected PathNormalizer $normalizer;
17
18    /**
19     * @see FlysystemBackCompatTrait::directoryExists()
20     */
21    public function getNormalizer(): PathNormalizer
22    {
23        if (!isset($this->normalizer)) {
24            $this->normalizer = new StripProtocolPathNormalizer(['mem'], new WhitespacePathNormalizer());
25        }
26        return $this->normalizer;
27    }
28
29    public function visibility(string $path): FileAttributes
30    {
31        if (!$this->fileExists($path)) {
32            // Assume it is a directory.
33
34//            Maybe check does the directory exist.
35//            $parentDirContents = (array) $this->listContents(dirname($path), false);
36//            throw UnableToRetrieveMetadata::visibility($path, 'file does not exist');
37
38            return new FileAttributes($path, null, 'public');
39        }
40
41
42        return parent::visibility($path);
43    }
44
45    public function lastModified(string $path): FileAttributes
46    {
47        if (!$this->fileExists($path)) {
48            // Assume it is a directory
49            return new FileAttributes($path, null, null, 0);
50        }
51
52        return parent::lastModified($path);
53    }
54
55    public function copy(string $source, string $destination, Config $config): void
56    {
57        $this->createDirectories($destination, $config);
58
59        parent::copy($source, $destination, $config);
60    }
61
62    public function write(string $path, string $contents, Config $config): void
63    {
64        // Make sure there is a directory for the file to be written to.
65        if (false === strpos($path, '______DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST')) {
66            $this->createDirectories($path, $config);
67        }
68
69        parent::write($path, $contents, $config);
70    }
71
72    protected function createDirectories(string $path, Config $config): void
73    {
74        $pathDirs = explode('/', dirname($path));
75        for ($level = 0; $level < count($pathDirs); $level++) {
76            $dir = implode('/', array_slice($pathDirs, 0, $level + 1));
77            $this->createDirectory($dir, $config);
78        }
79    }
80}