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.
Completed
Push — master ( 0b4676...ecfcbd )
by joseph
17s queued 14s
created

AbstractFilesystemItem::createSplFileInfo()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

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