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

CopyResourceCommand::__invoke()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 4.8196
cc 10
eloc 16
nc 6
nop 1
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
3
namespace Staticus\Resources\Commands;
4
5
use League\Flysystem\FilesystemInterface;
6
use Staticus\Resources\Exceptions\CommandErrorException;
7
use Staticus\Resources\ResourceDOInterface;
8
9
class CopyResourceCommand implements ResourceCommandInterface
10
{
11
    /**
12
     * @var ResourceDOInterface
13
     */
14
    protected $originResourceDO;
15
    /**
16
     * @var ResourceDOInterface
17
     */
18
    protected $newResourceDO;
19
    /**
20
     * @var FilesystemInterface
21
     */
22
    protected $filesystem;
23
24 14
    public function __construct(ResourceDOInterface $originResourceDO, ResourceDOInterface $newResourceDO, FilesystemInterface $filesystem)
25
    {
26 14
        $this->originResourceDO = $originResourceDO;
27 14
        $this->newResourceDO = $newResourceDO;
28 14
        $this->filesystem = $filesystem;
29 14
    }
30
31
    /**
32
     * @param bool $replace Replace exist file or just do nothing
33
     * @return ResourceDOInterface
34
     * @throws CommandErrorException
35
     */
36 14
    public function __invoke($replace = false)
37
    {
38 14
        if (!$this->originResourceDO->getName() || !$this->originResourceDO->getType()) {
39 1
            throw new CommandErrorException('Source resource cannot be empty');
40
        }
41 13
        if (!$this->newResourceDO->getName() || !$this->newResourceDO->getType()) {
42 1
            throw new CommandErrorException('Destination resource cannot be empty');
43
        }
44 12
        $originPath = $this->originResourceDO->getFilePath();
45 12
        $newPath = $this->newResourceDO->getFilePath();
46 12
        if ($originPath === $newPath) {
47 1
            throw new CommandErrorException('Source and destination paths is equal');
48
        }
49 11
        if (!$this->filesystem->has($originPath)) {
50 1
            throw new CommandErrorException('Origin file is not exists: ' . $originPath);
51
        }
52 10
        $exists = $this->filesystem->has($newPath);
53 10
        if (!$exists || $replace) {
54 9
            $this->copyFile($originPath, $newPath, $exists && $replace);
55
56 9
            return $this->newResourceDO;
57
        }
58
59 1
        return $this->originResourceDO;
60
    }
61
62 9
    protected function copyFile($fromFullPath, $toFullPath, $replace = false)
63
    {
64 9
        $this->createDirectory(dirname($toFullPath));
65 9
        if ($replace) {
66 1
            $this->filesystem->delete($toFullPath);
67 1
        }
68 9
        if (!$this->filesystem->copy($fromFullPath, $toFullPath)) {
69
            throw new CommandErrorException('File cannot be copied to the path ' . $toFullPath);
70
        }
71 9
    }
72
73 9
    protected function createDirectory($directory)
74
    {
75 9
        if (!$this->filesystem->createDir($directory)) {
76
            throw new CommandErrorException('Can\'t create a directory: ' . $directory);
77
        }
78
    }
79
}