Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.36% covered (warning)
86.36%
19 / 22
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlysystemBackCompatTrait
86.36% covered (warning)
86.36%
19 / 22
0.00% covered (danger)
0.00%
0 / 2
17.73
0.00% covered (danger)
0.00%
0 / 1
 directoryExists
89.47% covered (warning)
89.47%
17 / 19
0.00% covered (danger)
0.00%
0 / 1
13.20
 has
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
4.59
1<?php
2
3namespace BrianHenryIE\Strauss\Helpers;
4
5use League\Flysystem\FileAttributes;
6
7/**
8 * @see FlysystemBackCompatInterface
9 */
10trait FlysystemBackCompatTrait
11{
12
13    // Some version of Flysystem has:
14    // directoryExists
15    public function directoryExists(string $location): bool
16    {
17        if (method_exists($this, 'normalizePath')) {
18            $location = $this->normalizePath($location);
19        }
20
21        /**
22         * Use `self::class` here to check the parent of the current class, not necessarily the parent of the class
23         * which was called.
24         */
25//        if (get_parent_class(self::class) && method_exists(get_parent_class(self::class), 'directoryExists')) {
26//            return parent::directoryExists($location);
27//        }
28
29        if (property_exists($this, 'flysystemAdapter') && method_exists($this->flysystemAdapter, 'directoryExists')) {
30            return $this->flysystemAdapter->directoryExists($location);
31        }
32
33        if (property_exists($this, 'filesystem') && method_exists($this->filesystem, 'directoryExists')) {
34            return $this->filesystem->directoryExists($location);
35        }
36
37        $parentDir = dirname($location);
38        if (method_exists($this, 'normalizePath')) {
39            $parentDir = $this->normalizePath($parentDir);
40        }
41        $parentDir = $parentDir === '.' ? '/' : $parentDir;
42        $parentDirectoryContents = $this->listContents($parentDir, false);
43        /** @var FileAttributes $entry */
44        foreach ($parentDirectoryContents as $entry) {
45            if ($entry->path() == $location) {
46                return $entry->isDir();
47            }
48        }
49
50        // symlinks.
51        if (property_exists($this, 'pathPrefixer')) {
52            if (false !== realpath($this->pathPrefixer->prefixPath($location))
53                && is_dir($this->pathPrefixer->prefixPath($location))) {
54                return true;
55            }
56        }
57
58        return false;
59    }
60
61    // Some version of Flysystem has:
62    // has
63    public function has(string $location): bool
64    {
65        if (get_parent_class(self::class) && method_exists(get_parent_class(self::class), 'has')) {
66            return parent::has($location);
67        }
68        return $this->fileExists($location) || $this->directoryExists($location);
69    }
70}