GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#214)
by joseph
20:33
created

AbstractFilesystemItem::exists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem;
4
5
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
6
use RuntimeException;
7
use SplFileInfo;
8
use function realpath;
9
10
abstract class AbstractFilesystemItem
11
{
12
    /**
13
     * This is the name of the path type. It should be overridden in child classes.
14
     *
15
     * @var string
16
     */
17
    protected const PATH_TYPE = 'override me';
18
19
    /**
20
     * This is the path as configured for the filesystem item. It in no way means the item exists there, it is simply
21
     * where we expect it to exist if/when it has been created
22
     *
23
     * @var string
24
     */
25
    protected $path;
26
27
    /**
28
     * What permissions should be set to created filesystem items?
29
     *
30
     * @var int
31
     */
32
    protected $createModeOctal = 0777;
33
34
    /**
35
     * A string representation of the octal number
36
     *
37
     * @var string
38
     */
39
    protected $createModeOctalString = '0777';
40
41
    /**
42
     * Stores an instance fo the SplFileInfo object for the path item.
43
     *
44
     * @var SplFileInfo
45
     */
46
    protected $splFileInfo;
47
48
    /**
49
     * @param string $path - the path where we would expect the item to exist if/when it has been created
50
     */
51 7
    public function __construct(string $path = null)
52
    {
53 7
        if (static::PATH_TYPE === self::PATH_TYPE) {
0 ignored issues
show
introduced by
The condition static::PATH_TYPE === self::PATH_TYPE is always true.
Loading history...
54
            throw new RuntimeException('You must override the PATH_TYPE in your concrete class');
55
        }
56 7
        if (null !== $path) {
57 4
            $this->setPath($path);
58
        }
59 7
    }
60
61
    /**
62
     * Create the filesystem item, asserting that it does not already exist
63
     *
64
     * @return static
65
     * @throws DoctrineStaticMetaException
66
     */
67 3
    public function create()
68
    {
69 3
        $this->assertPathIsSet();
70 1
        $this->assertNotExists();
71 1
        $this->doCreate();
72
        $this->setPermissions();
73
74
        return $this;
75
    }
76
77
    /**
78
     * @throws DoctrineStaticMetaException
79
     */
80 17
    protected function assertPathIsSet(): void
81
    {
82 17
        if (null === $this->path) {
83 9
            throw new DoctrineStaticMetaException('$this->path is not set');
84
        }
85 8
    }
86
87
    /**
88
     * Throw an Exception if there is already something that exists at the path
89
     *
90
     * @throws DoctrineStaticMetaException
91
     */
92 1
    protected function assertNotExists(): void
93
    {
94 1
        if (true === $this->exists()) {
95
            throw new DoctrineStaticMetaException(static::PATH_TYPE . ' already exists at path ' . $this->path);
96
        }
97 1
    }
98
99
    /**
100
     * Check if something exists at the real path
101
     *
102
     * @return bool
103
     */
104 11
    public function exists(): bool
105
    {
106 11
        $realPath = realpath($this->path);
107 11
        if (false === $realPath) {
108 10
            return false;
109
        }
110 1
        $this->path = $realPath;
111 1
        $this->assertCorrectType();
112
113
        return true;
114
    }
115
116
    /**
117
     * Check that the path is actually a file/directory etc
118
     *
119
     * @return void
120
     */
121 1
    protected function assertCorrectType(): void
122
    {
123 1
        if (false === $this->isCorrectType()) {
124 1
            throw new RuntimeException('path is not the correct type: ' . $this->path);
125
        }
126
    }
127
128
    abstract protected function isCorrectType(): bool;
129
130
    /**
131
     * This is the specific creation logic for the concrete filesystem type.
132
     */
133
    abstract protected function doCreate(): void;
134
135
    private function setPermissions(): void
136
    {
137
        chmod($this->path, $this->createModeOctal);
138
    }
139
140
    /**
141
     * Get the path to the filesystem item as it is currently set in this object
142
     *
143
     * @return null|string
144
     */
145 6
    public function getPath(): ?string
146
    {
147 6
        return $this->path;
148
    }
149
150
    /**
151
     * Set the string path to the filesystem item
152
     *
153
     * @param string $path
154
     *
155
     * @return $this
156
     */
157 5
    public function setPath(string $path)
158
    {
159 5
        $this->path = $path;
160
161 5
        return $this;
162
    }
163
164
    /**
165
     * Provide an SplFileInfo object, asserting that the path exists
166
     *
167
     * @return SplFileInfo
168
     * @throws DoctrineStaticMetaException
169
     */
170 8
    public function getSplFileInfo(): SplFileInfo
171
    {
172 8
        if (null !== $this->splFileInfo && $this->path === $this->splFileInfo->getRealPath()) {
173
            return $this->splFileInfo;
174
        }
175 8
        $this->assertExists();
176
177
        return $this->createSplFileInfo();
178
    }
179
180 15
    protected function assertExists(): void
181
    {
182 15
        $this->assertPathIsSet();
183 8
        if (false === $this->exists()) {
184 8
            throw new DoctrineStaticMetaException(static::PATH_TYPE . ' does not exist at path ' . $this->path);
185
        }
186
    }
187
188
    /**
189
     * Create an SplFileInfo, assuming the path already exists
190
     *
191
     * @return SplFileInfo
192
     */
193 1
    protected function createSplFileInfo(): SplFileInfo
194
    {
195 1
        if (null !== $this->splFileInfo && $this->path === $this->splFileInfo->getRealPath()) {
196
            return $this->splFileInfo;
197
        }
198 1
        $this->splFileInfo = new SplFileInfo($this->path);
199
200 1
        return $this->splFileInfo;
201
    }
202
203
    /**
204
     * @param int $createMode
205
     *
206
     * @return static
207
     */
208
    public function setCreateMode(int $createMode): self
209
    {
210
        $this->createModeOctal       = $createMode;
211
        $this->createModeOctalString = decoct($createMode);
212
213
        return $this;
214
    }
215
}
216