Processor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace AppBundle\Sync;
3
4
use AppBundle\Exception\TaskException;
5
use AppBundle\Sync\Entity\Task;
6
use AppBundle\Sync\Entity\Task\Add;
7
use AppBundle\Sync\Entity\Task\Delete;
8
use AppBundle\Sync\Entity\Task\Update;
9
use AppBundle\Sync\Storage\StorageInterface;
10
11
/**
12
 * Task processor
13
 *
14
 * @author Sergey Sadovoi <[email protected]>
15
 */
16
class Processor
17
{
18
    /**
19
     * @var StorageInterface  Storage for task
20
     */
21
    protected $storage;
22
23
    /**
24
     * Init the storage
25
     *
26
     * @param StorageInterface $storage
27
     */
28 5
    public function __construct(StorageInterface $storage)
29
    {
30 5
        $this->storage = $storage;
31 5
    }
32
33
    /**
34
     * Executes task
35
     *
36
     * @param Task $task
37
     *
38
     * @return string  Result message
39
     *
40
     * @throws TaskException
41
     */
42 5
    public function execute(Task $task)
43
    {
44 5
        $method = $task->getName();
45 5
        if (!method_exists($this, $method)) {
46 1
            throw new TaskException(sprintf('Can\'t process {%s} task', $method), TaskException::INVALID_TASK);
47
        }
48
49 4
        $this->$method($task);
50
51 4
        return $task->getMessageSuccess();
52
    }
53
54
    /**
55
     * Execute Add task
56
     *
57
     * @param Add $task
58
     */
59 2
    protected function add(Add $task)
60
    {
61 2
        $this->storage->put($task->getSourcePath(), $task->getDestPath());
62 2
    }
63
64
    /**
65
     * Execute Delete task
66
     *
67
     * @param Delete $task
68
     */
69 2
    protected function delete(Delete $task)
70
    {
71 2
        $this->storage->delete($task->getDestPath());
72 2
    }
73
74
    /**
75
     * Execute Update task
76
     *
77
     * @param Update $task
78
     */
79 2
    protected function update(Update $task)
80
    {
81 2
        $this->storage->put($task->getSourcePath(), $task->getDestPath());
82 2
    }
83
}
84