Passed
Push — master ( d2829c...baba2d )
by Marek
02:45
created

UpdateTicketTask::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace AppBuilder\Application\Module\TaskManager\Task;
6
7
use AppBuilder\Application\Configuration\ValueObject\Parameters;
8
use AppBuilder\Application\Model\ValueObject\Ticket;
9
use AppBuilder\Application\Utils\FileManager\FileManagerService;
10
use Nette\DirectoryNotFoundException;
11
use Symfony\Component\Filesystem\Exception\IOException;
12
13
class UpdateTicketTask implements Task
14
{
15
    /** @var string */
16
    private $path;
17
18
    /** @var Ticket */
19
    private $ticket;
20
21
    /** @var Parameters */
22
    private $applicationParams;
23
24
    /** @var FileManagerService */
25
    private $fileManager;
26
27 1
    public function __construct(Ticket $ticket, Parameters $applicationParams, FileManagerService $fileManager)
28
    {
29 1
        $this->applicationParams = $applicationParams;
30 1
        $this->ticket            = $ticket;
31 1
        $this->path              = $applicationParams->path($ticket->key());
32 1
        $this->fileManager       = $fileManager;
33 1
    }
34
35
    /**
36
     * Updates application code, if tickets status changes to 'work finished'.
37
     *
38
     * @throws DirectoryNotFoundException
39
     * @throws IOException
40
     */
41 1
    public function execute() : bool
42
    {
43
        return
44 1
            $this->executeGitPull($this->path)
45 1
            && $this->createSnapshot()
46 1
            && $this->executeComposerInstall($this->path);
47
    }
48
49
    /**
50
     * Runs git clone command in ticket directory. Returns true when process is finished.
51
     * Uses SSH key for authorization.
52
     *
53
     * @throws DirectoryNotFoundException
54
     */
55 1
    private function executeGitPull(string $path) : bool
56
    {
57 1
        $pullCommand = $this->combinePullCommand($this->ticket->branch());
58 1
        $this->fileManager->changeDir($path);
59 1
        exec($pullCommand);
60
61 1
        return true;
62
    }
63
64
    /**
65
     * Combines string with valid git pull command.
66
     */
67 1
    private function combinePullCommand(string $branch) : string
68
    {
69 1
        return 'git pull origin ' . $branch;
70
    }
71
72
    /**
73
     * Creates file with tickets current status and timestamp inside ticket dir.
74
     *
75
     * @throws IOException
76
     */
77 1
    private function createSnapshot() : bool
78
    {
79 1
        $snapshotPath = $this->applicationParams->snapshotPath($this->ticket->key());
80 1
        $snap         = $this->ticket->ticketStatus() . ';' . date('Y-m-d h:m:s');
81 1
        $this->fileManager->filePutContent($snapshotPath, $snap);
82
83 1
        return true;
84
    }
85
86
    /**
87
     * Runs composer install command in ticket directory.
88
     * Returns true when process is finished.
89
     */
90 1
    private function executeComposerInstall(string $path) : bool
91
    {
92 1
        exec('composer install --working-dir=' . $path);
93
94 1
        return true;
95
    }
96
}
97