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

CopyResourceCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.94%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 16
c 5
b 0
f 0
lcom 1
cbo 3
dl 0
loc 71
ccs 31
cts 33
cp 0.9394
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
D __invoke() 0 25 10
A copyFile() 0 10 3
A createDirectory() 0 6 2
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
}