DestroyResourceCommand::__invoke()   C
last analyzed

Complexity

Conditions 16
Paths 3

Size

Total Lines 45
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 16.0059

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 45
ccs 34
cts 35
cp 0.9714
rs 5.0151
cc 16
eloc 32
nc 3
nop 1
crap 16.0059

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Staticus\Resources\Commands;
3
4
use League\Flysystem\FilesystemInterface;
5
use Staticus\Resources\Exceptions\CommandErrorException;
6
use Staticus\Resources\ResourceDOAbstract;
7
use Staticus\Resources\ResourceDOInterface;
8
9
class DestroyResourceCommand implements ResourceCommandInterface
10
{
11
    /**
12
     * @var ResourceDOInterface
13
     */
14
    protected $resourceDO;
15
    /**
16
     * @var FilesystemInterface
17
     */
18
    protected $filesystem;
19
20 13
    public function __construct(ResourceDOInterface $resourceDO, FilesystemInterface $filesystem)
21
    {
22 13
        $this->resourceDO = $resourceDO;
23 13
        $this->filesystem = $filesystem;
24 13
    }
25
26
    /**
27
     * @param bool $byPathOnly If true, no search on disk will be executed
28
     * @return ResourceDOInterface
29
     */
30 13
    public function __invoke($byPathOnly = false)
31
    {
32 13
        $uuid = $this->resourceDO->getUuid();
33 13
        $type = $this->resourceDO->getType();
34 13
        $variant = $this->resourceDO->getVariant();
35 13
        $version = $this->resourceDO->getVersion();
36 13
        $baseDir = $this->resourceDO->getBaseDirectory();
37 13
        $namespace = $this->resourceDO->getNamespace();
38 13
        $filePath = $this->resourceDO->getFilePath();
39 13
        if (!$uuid || !$type || !$baseDir || !$filePath) {
40 1
            throw new CommandErrorException('Cannot destroy the empty resource');
41
        }
42 12
        if ($byPathOnly) {
43 6
            $this->deleteFile($filePath);
44 6
        } else {
45 6
            $command = new FindResourceOptionsCommand($this->resourceDO, $this->filesystem);
46 6
            $result = $command();
47 6
            foreach ($result as $item) {
48
                if (
49 6
                    $item[ResourceDOAbstract::TOKEN_TYPE] !== $type
50 6
                    || $item['filename'] !== $uuid
51 6
                    || ($namespace && ($item[ResourceDOAbstract::TOKEN_NAMESPACE] !== $namespace))
52 6
                ) {
53
                    continue;
54
                }
55 6
                if ($version !== ResourceDOInterface::DEFAULT_VERSION) {
56
                    if (
57
                        // delete versions only for current variant
58 2
                        $variant === $item[ResourceDOAbstract::TOKEN_VARIANT]
59 2
                        && $version === (int)$item[ResourceDOAbstract::TOKEN_VERSION]
60 2
                    ) {
61 2
                        $this->deleteFile($item['path']);
62 2
                    }
63 6
                } elseif ($variant !== ResourceDOInterface::DEFAULT_VARIANT) {
64 2
                    if ($variant === $item[ResourceDOAbstract::TOKEN_VARIANT]) {
65 2
                        $this->deleteFile($item['path']);
66 2
                    }
67 2
                } else {
68 2
                    $this->deleteFile($item['path']);
69
                }
70 6
            }
71
        }
72
73 12
        return $this->resourceDO;
74
    }
75
76 12
    protected function deleteFile($filePath)
77
    {
78
        // If file is already gone somewhere, it is OK for us
79 12
        if ($this->filesystem->has($filePath) && !$this->filesystem->delete($filePath)) {
80
            throw new CommandErrorException('The file cannot be removed: ' . $filePath);
81
        }
82
    }
83
}