Passed
Branch master (267be1)
by Eugene
03:26
created

DeleteSafetyResourceCommand::__invoke()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 38
ccs 22
cts 22
cp 1
rs 5.3846
cc 8
eloc 20
nc 6
nop 0
crap 8
1
<?php
2
namespace Staticus\Resources\Commands;
3
4
use League\Flysystem\FilesystemInterface;
5
use Staticus\Resources\Exceptions\CommandErrorException;
6
use Staticus\Resources\ResourceDOInterface;
7
8
class DeleteSafetyResourceCommand implements ResourceCommandInterface
9
{
10
    /**
11
     * @var ResourceDOInterface
12
     */
13
    protected $resourceDO;
14
    /**
15
     * @var FilesystemInterface
16
     */
17
    protected $filesystem;
18
19 7
    public function __construct(ResourceDOInterface $resourceDO, FilesystemInterface $filesystem)
20
    {
21 7
        $this->resourceDO = $resourceDO;
22 7
        $this->filesystem = $filesystem;
23 7
    }
24
25 7
    public function __invoke()
26
    {
27 7
        $version = $this->resourceDO->getVersion();
28 7
        $filePath = $this->resourceDO->getFilePath();
29 7
        if (!$this->resourceDO->getName() || !$this->resourceDO->getType() || !$this->resourceDO->getBaseDirectory()) {
30 1
            throw new CommandErrorException('Cannot delete empty resource');
31
        }
32 6
        if ($this->filesystem->has($filePath)) {
33
34
            // Make backup of the default version
35 5
            if (ResourceDOInterface::DEFAULT_VERSION === $version) {
36 4
                $lastVersion = $this->findLastVersion();
37
38
                // But only if previous existing version is not the default and not has the same content as deleting
39 4
                if (ResourceDOInterface::DEFAULT_VERSION !== $lastVersion) {
40 2
                    $lastVersionResourceDO = clone $this->resourceDO;
41 2
                    $lastVersionResourceDO->setVersion($lastVersion);
42 2
                    $command = new DestroyEqualResourceCommand($lastVersionResourceDO, $this->resourceDO, $this->filesystem);
43 2
                    $result = $command();
44 2
                    if ($result === $this->resourceDO) {
45
46
                        // If the previous file version already the same, current version is already deleted
47
                        // and backup and yet another deletion is not needed anymore
48 1
                        return $this->resourceDO;
49
                    }
50 1
                }
51
52 3
                $command = new BackupResourceCommand($this->resourceDO, $this->filesystem);
53 3
                $command($lastVersion);
54 3
            }
55
56 4
            $this->deleteFile($filePath);
57
58 4
            return $this->resourceDO;
59
        }
60
61 1
        return $this->resourceDO;
62
    }
63
64
    /**
65
     * @param $filePath
66
     */
67 4
    protected function deleteFile($filePath)
68
    {
69 4
        if (!$this->filesystem->delete($filePath)) {
70
            throw new CommandErrorException('The file cannot be removed: ' . $filePath);
71
        }
72 4
    }
73
74
    /**
75
     * @return int
76
     */
77 4
    protected function findLastVersion()
78
    {
79 4
        $command = new FindResourceLastVersionCommand($this->resourceDO, $this->filesystem);
80
81 4
        return $command();
82
    }
83
}