Passed
Push — master ( 0057b2...3e8fcf )
by Marek
02:11
created

CreateTicketDirTask::execute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 5
nop 0
crap 5
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 FileNotFoundException;
11
use Nette\DirectoryNotFoundException;
12
use Symfony\Component\Filesystem\Exception\IOException;
13
14
class CreateTicketDirTask implements Task
15
{
16
    /** @var Ticket */
17
    private $ticket;
18
19
    /** @var Parameters */
20
    private $applicationParams;
21
22
    /** @var string */
23
    private $path;
24
25
    /** @var FileManagerService */
26
    private $fileManager;
27
28 3
    public function __construct(Ticket $ticket, Parameters $applicationParams, FileManagerService $fileManager)
29
    {
30 3
        $this->ticket            = $ticket;
31 3
        $this->applicationParams = $applicationParams;
32 3
        $this->path              = $applicationParams->path($ticket->key());
33 3
        $this->fileManager       = $fileManager;
34 3
    }
35
36
    /**
37
     * Creates directory for application, deploys it and installs symlinks.
38
     *
39
     * @throws FileNotFoundException
40
     * @throws DirectoryNotFoundException
41
     * @throws IOException
42
     */
43 3
    public function execute() : bool
44
    {
45 3
        return $this->createDirectory()
46 2
            && $this->executeGitClone()
47 1
            && $this->createSnapshot()
48 1
            && $this->executeComposerInstall()
49 1
            && $this->installSymlinks();
50
    }
51
52
    /**
53
     * Creates directory for application
54
     *
55
     * @throws FileNotFoundException
56
     */
57 3
    private function createDirectory() : bool
58
    {
59 3
        if (!$this->fileManager->createDir($this->path)) {
60 1
            throw new FileNotFoundException(
61 1
                'Could not create directory for ' . $this->ticket->key()
62
            );
63
        }
64
65 2
        return true;
66
    }
67
68
    /**
69
     * Runs git clone command in application directory. Returns true when process is finished.
70
     * Uses SSH key for authorization.
71
     *
72
     * @throws DirectoryNotFoundException
73
     */
74 2
    private function executeGitClone() : bool
75
    {
76 2
        $this->fileManager->changeDir($this->path);
77 1
        exec($this->combineCloneCommand());
78
79 1
        return true;
80
    }
81
82
    /**
83
     * Returns git clone bash command combined for particular application.
84
     */
85 1
    private function combineCloneCommand() : string
86
    {
87
        return 'git clone '
88 1
            . $this->applicationParams->bitbucketRepositoryHost()
89 1
            . $this->ticket->repository()
90 1
            . '.git -b '
91 1
            . $this->ticket->branch()
92 1
            . ' '
93 1
            . $this->path;
94
    }
95
96
    /**
97
     * Creates file with tickets current status and timestamp inside ticket dir.
98
     */
99 1
    private function createSnapshot() : bool
100
    {
101 1
        $snapshotPath = $this->applicationParams->snapshotPath($this->ticket->key());
102 1
        $snap         = $this->ticket->ticketStatus() . ';' . date('Y-m-d h:m:s');
103 1
        $this->fileManager->filePutContent($snapshotPath, $snap);
104
105 1
        return true;
106
    }
107
108
    /**
109
     * Runs composer install command in ticket directory.
110
     * Returns true when process is finished.
111
     */
112 1
    private function executeComposerInstall() : bool
113
    {
114 1
        exec('composer install --working-dir=' . $this->path);
115
116 1
        return true;
117
    }
118
119
    /**
120
     * Installs symlinks for application.
121
     *
122
     * @throws IOException
123
     */
124 1
    private function installSymlinks() : bool
125
    {
126 1
        $ticketKey = $this->ticket->key();
127
128 1
        $this->fileManager->createSymlink(
129 1
            $this->applicationParams->symlinkTarget($ticketKey),
130 1
            $this->applicationParams->symlinkSource($ticketKey)
131
        );
132
133 1
        return true;
134
    }
135
}
136