Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
15 / 18
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
InMemoryFilesystemAdapter
83.33% covered (warning)
83.33%
15 / 18
83.33% covered (warning)
83.33%
5 / 6
11.56
0.00% covered (danger)
0.00%
0 / 1
 normalizePath
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
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 copy
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 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\Flysystem;
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 FlysystemAdapterBackCompatTraitInterface
13{
14    use FlysystemAdapterBackCompatTrait;
15
16    protected PathNormalizer $normalizer;
17
18    /**
19     * @see FlysystemReaderBackCompatTrait::directoryExists()
20     */
21    public function normalizePath(string $path): string
22    {
23        if (!isset($this->normalizer)) {
24            $this->normalizer = new StripProtocolPathNormalizer(['mem'], new WhitespacePathNormalizer());
25        }
26        return $this->normalizer->normalizePath($path);
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    /**
63     * @see \League\Flysystem\FilesystemAdapter::write()
64     */
65    public function write(string $path, string $contents, Config $config): void
66    {
67        // Make sure there is a directory for the file to be written to.
68        if (false === strpos($path, '______DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST')) {
69            $this->createDirectories($path, $config);
70        }
71
72        parent::write($path, $contents, $config);
73    }
74
75    protected function createDirectories(string $path, Config $config): void
76    {
77        $pathDirs = explode('/', dirname($path));
78        for ($level = 0; $level < count($pathDirs); $level++) {
79            $dir = implode('/', array_slice($pathDirs, 0, $level + 1));
80            $this->createDirectory($dir, $config);
81        }
82    }
83}