Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlysystemAdapterBackCompatTrait
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 2
11.17
0.00% covered (danger)
0.00%
0 / 1
 directoryExists
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 directoryExistsImplementation
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
8.02
1<?php
2/**
3 * `FilesystemAdapter` interface v3 introduced `::directoryExists()`.
4 *
5 * v2:
6 * @see https://github.com/thephpleague/flysystem/blob/2.x/src/FilesystemAdapter.php
7 * Interface:
8 * @see https://github.com/thephpleague/flysystem/blob/3.x/src/FilesystemAdapter.php
9 * Implementations:
10 * @see https://github.com/thephpleague/flysystem/blob/3.x/src/Filesystem.php#L41-L44
11 * @see https://github.com/thephpleague/flysystem/blob/3.x/src/Local/LocalFilesystemAdapter.php#L346-L351
12 */
13
14namespace BrianHenryIE\Strauss\Helpers\Flysystem;
15
16use League\Flysystem\FileAttributes;
17use League\Flysystem\FilesystemAdapter;
18use League\Flysystem\FilesystemException;
19
20/**
21 * @mixin FlysystemAdapterBackCompatTraitInterface
22 *
23 * @method string normalizePath($location)
24 */
25trait FlysystemAdapterBackCompatTrait
26{
27    /**
28     * @see FilesystemAdapter::directoryExists()
29     * @param string $path
30     *
31     * @return bool
32     * @throws FilesystemException
33     */
34    public function directoryExists(string $path): bool
35    {
36        /**
37         * Use `self::class` here to check the parent of the current class, not necessarily the parent of the class
38         * which was called.
39         *
40         * @phpstan-ignore booleanAnd.leftAlwaysTrue
41         */
42        if (get_parent_class(self::class) && method_exists(get_parent_class(self::class), 'directoryExists')) {
43            /** @phpstan-ignore staticMethod.notFound  */
44            return parent::directoryExists($path);
45        }
46
47        return $this->directoryExistsImplementation($path);
48    }
49
50    /**
51     * @throws FilesystemException
52     */
53    protected function directoryExistsImplementation(string $path): bool
54    {
55        $path = $this->normalizePath($path);
56
57        $parentDir = dirname($path);
58        $parentDir = $this->normalizePath($parentDir);
59        $parentDir = $parentDir === '.' ? '/' : $parentDir;
60
61        // Root dir must exist!
62        if ($parentDir === $path) {
63            return true;
64        }
65
66        $parentDirectoryContents = $this->listContents($parentDir, false);
67        /** @var FileAttributes $entry */
68        foreach ($parentDirectoryContents as $entry) {
69            if ($entry->path() == $path) {
70                return $entry->isDir();
71            }
72        }
73
74        // Symlinks.
75        // TODO: This should be moved into its own adapter.
76        if (property_exists($this, 'pathPrefixer')) {
77            if (false !== realpath($this->pathPrefixer->prefixPath($path))
78                && is_dir($this->pathPrefixer->prefixPath($path))) {
79                return true;
80            }
81        }
82
83        return false;
84    }
85}