ResourceExistenceChecker   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 117
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B checkResources() 0 24 7
B checkResource() 0 23 8
A checkSourceAndTargetResource() 0 14 3
A isDir() 0 4 1
A isFile() 0 4 1
1
<?php
2
3
namespace Robo\Common;
4
5
trait ResourceExistenceChecker
6
{
7
    /**
8
     * Checks if the given input is a file or folder.
9
     *
10
     * @param string|string[] $resources
11
     * @param string $type
12
     *   Allowed values: "file", "dir", "fileAndDir"
13
     *
14
     * @return bool
15
     *   True if no errors were encountered otherwise false.
16
     */
17
    protected function checkResources($resources, $type = 'fileAndDir')
18
    {
19
        if (!in_array($type, ['file', 'dir', 'fileAndDir'])) {
20
            throw new \InvalidArgumentException(sprintf('Invalid resource check of type "%s" used!', $type));
21
        }
22
        if (is_string($resources)) {
23
            $resources = [$resources];
24
        }
25
        $success = true;
26
        foreach ($resources as $resource) {
27
            $glob = glob($resource);
28
            if ($glob === false) {
29
                $this->printTaskError(sprintf('Invalid glob "%s"!', $resource), $this);
0 ignored issues
show
Bug introduced by
It seems like printTaskError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
                $success = false;
31
                continue;
32
            }
33
            foreach ($glob as $resource) {
34
                if (!$this->checkResource($resource, $type)) {
35
                    $success = false;
36
                }
37
            }
38
        }
39
        return $success;
40
    }
41
42
    /**
43
     * Checks a single resource, file or directory.
44
     *
45
     * It will print an error as well on the console.
46
     *
47
     * @param string $resource
48
     *   File or folder.
49
     * @param string $type
50
     *   Allowed values: "file", "dir", "fileAndDir".
51
     *
52
     * @return bool
53
     */
54
    protected function checkResource($resource, $type)
55
    {
56
        switch ($type) {
57
            case 'file':
58
                if (!$this->isFile($resource)) {
59
                    $this->printTaskError(sprintf('File "%s" does not exist!', $resource), $this);
0 ignored issues
show
Bug introduced by
It seems like printTaskError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
                    return false;
61
                }
62
                return true;
63
            case 'dir':
64
                if (!$this->isDir($resource)) {
65
                    $this->printTaskError(sprintf('Directory "%s" does not exist!', $resource), $this);
0 ignored issues
show
Bug introduced by
It seems like printTaskError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
                    return false;
67
                }
68
                return true;
69
            case 'fileAndDir':
70
                if (!$this->isDir($resource) && !$this->isFile($resource)) {
71
                    $this->printTaskError(sprintf('File or directory "%s" does not exist!', $resource), $this);
0 ignored issues
show
Bug introduced by
It seems like printTaskError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
                    return false;
73
                }
74
                return true;
75
        }
76
    }
77
78
    /**
79
     * Convenience method to check the often uses "source => target" file / folder arrays.
80
     *
81
     * @param string|array $resources
82
     */
83
    protected function checkSourceAndTargetResource($resources)
84
    {
85
        if (is_string($resources)) {
86
            $resources = [$resources];
87
        }
88
        $sources = [];
89
        $targets = [];
90
        foreach ($resources as $source => $target) {
91
            $sources[] = $source;
92
            $target[] = $target;
93
        }
94
        $this->checkResources($sources);
95
        $this->checkResources($targets);
96
    }
97
98
   /**
99
    * Wrapper method around phps is_dir()
100
    *
101
    * @param string $directory
102
    *
103
    * @return bool
104
    */
105
    protected function isDir($directory)
106
    {
107
        return is_dir($directory);
108
    }
109
110
   /**
111
    * Wrapper method around phps file_exists()
112
    *
113
    * @param string $file
114
    *
115
    * @return bool
116
    */
117
    protected function isFile($file)
118
    {
119
        return file_exists($file);
120
    }
121
}
122