DestroyResourceCommand   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.35%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 20
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 75
ccs 41
cts 43
cp 0.9535
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A deleteFile() 0 7 3
C __invoke() 0 45 16
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
}