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

DestroyEqualResourceCommand::__invoke()   D

Complexity

Conditions 10
Paths 5

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 34
ccs 20
cts 20
cp 1
rs 4.8196
cc 10
eloc 20
nc 5
nop 0
crap 10

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\ResourceDOInterface;
7
8
class DestroyEqualResourceCommand implements ResourceCommandInterface
9
{
10
    const HASH_ALGORITHM = 'md5';
11
    /**
12
     * @var ResourceDOInterface
13
     */
14
    protected $originResourceDO;
15
    /**
16
     * @var ResourceDOInterface
17
     */
18
    protected $suspectResourceDO;
19
    /**
20
     * @var FilesystemInterface
21
     */
22
    protected $filesystem;
23
24
    /**
25
     * @param ResourceDOInterface $originResourceDO This is a model resource for comparing
26
     * @param ResourceDOInterface $suspectResourceDO This resource will be deleted, if it's equal to $originResourceDO
27
     * @param FilesystemInterface $filesystem
28
     */
29 7
    public function __construct(ResourceDOInterface $originResourceDO, ResourceDOInterface $suspectResourceDO, FilesystemInterface $filesystem)
30
    {
31 7
        $this->originResourceDO = $originResourceDO;
32 7
        $this->suspectResourceDO = $suspectResourceDO;
33 7
        $this->filesystem = $filesystem;
34 7
    }
35
36
    /**
37
     * @return ResourceDOInterface SuspectResource if it have been deleted or OriginResource if the Suspect is not equal
38
     */
39 7
    public function __invoke()
40
    {
41 7
        $originName = $this->originResourceDO->getName();
42 7
        $suspectName = $this->suspectResourceDO->getName();
43 7
        $originType = $this->originResourceDO->getType();
44 7
        $suspectType = $this->suspectResourceDO->getType();
45 7
        $originFilePath = $this->originResourceDO->getFilePath();
46 7
        $suspectFilePath = $this->suspectResourceDO->getFilePath();
47
48 7
        if (!$originName || !$originType) {
49 1
            throw new CommandErrorException('Cannot destroy equal resource: the origin resource is empty');
50
        }
51 6
        if (!$suspectName || !$suspectType) {
52 1
            throw new CommandErrorException('Cannot destroy equal resource: the suspect resource is empty');
53
        }
54 5
        if ($originFilePath === $suspectFilePath) {
55 1
            throw new CommandErrorException('Cannot destroy equal resource: Origin and Suspect have same paths');
56
        }
57
58
        // Unfortunately, this condition can not always work fine.
59
        // Because some Middlewares can compress, resize etc. the resource that saved before
60
        // and the second uploaded copy will never be equal
61
        if ($originType === $suspectType
62 4
            && $this->filesystem->has($originFilePath) === $this->filesystem->has($suspectFilePath)
63 4
            && $this->filesystem->getSize($originFilePath) === $this->filesystem->getSize($suspectFilePath)
64 4
            && $this->getFileHash($originFilePath) === $this->getFileHash($suspectFilePath)
65 4
        ) {
66 2
            $command = new DestroyResourceCommand($this->suspectResourceDO, $this->filesystem);
67
68 2
            return $command(true);
69
        }
70
71 2
        return $this->originResourceDO;
72
    }
73
74 2
    public function getFileHash($path)
75
    {
76 2
        $stream = $this->filesystem->readStream($path);
77 2
        if ($stream !== false) {
78 2
            $context = hash_init(self::HASH_ALGORITHM);
79 2
            hash_update_stream($context, $stream);
80
81 2
            return hash_final($context);
82
        }
83
84
        return false;
85
    }
86
}