Completed
Branch master (d2829c)
by Marek
05:16 queued 02:32
created

CreateTicketDirTask::__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 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() : void
44
    {
45 3
        $this->createDirectory();
46 2
        $this->executeGitClone();
47 1
        $this->createSnapshot();
48 1
        $this->executeComposerInstall();
49 1
        $this->installSymlinks();
50 1
    }
51
52
    /**
53
     * Creates directory for application
54
     *
55
     * @throws FileNotFoundException
56
     */
57 3
    private function createDirectory() : void
58
    {
59 3
        if ($this->fileManager->createDir($this->path . $this->applicationParams->projectsHomeDir())) {
60 1
            throw new FileNotFoundException(
61
                'Could not create directory for '
62 1
                . $this->ticket->key()
63
            );
64
        }
65 2
    }
66
67
    /**
68
     * Runs git clone command in application directory. Returns true when process is finished.
69
     * Uses SSH key for authorization.
70
     *
71
     * @throws DirectoryNotFoundException
72
     */
73 2
    private function executeGitClone() : void
74
    {
75 2
        $this->fileManager->changeDir($this->path);
76 1
        exec($this->combineCloneCommand());
77 1
    }
78
79
    /**
80
     * Returns git clone bash command combined for particular application.
81
     */
82 1
    private function combineCloneCommand() : string
83
    {
84
        return 'git clone '
85 1
            . $this->applicationParams->bitbucketRepositoryHost()
86 1
            . $this->ticket->repository()
87 1
            . '.git -b '
88 1
            . $this->ticket->branch()
89 1
            . ' '
90 1
            . $this->path;
91
    }
92
93
    /**
94
     * Creates file with tickets current status and timestamp inside ticket dir.
95
     */
96 1
    private function createSnapshot() : void
97
    {
98 1
        $snapshotPath = $this->applicationParams->snapshotPath($this->ticket->key());
99 1
        $snap         = $this->ticket->ticketStatus() . ';' . date('Y-m-d h:m:s');
100 1
        $this->fileManager->filePutContent($snapshotPath, $snap);
101 1
    }
102
103
    /**
104
     * Runs composer install command in ticket directory.
105
     * Returns true when process is finished.
106
     */
107 1
    private function executeComposerInstall() : bool
108
    {
109 1
        exec('composer install --working-dir=' . $this->path);
110
111 1
        return true;
112
    }
113
114
    /**
115
     * Installs symlinks for application.
116
     *
117
     * @throws IOException
118
     */
119 1
    private function installSymlinks() : bool
120
    {
121 1
        $ticketKey = $this->ticket->key();
122
123 1
        $this->fileManager->createSymlink(
124 1
            $this->applicationParams->symlinkTarget($ticketKey),
125 1
            $this->applicationParams->symlinkSource($ticketKey)
126
        );
127
128 1
        return true;
129
    }
130
}
131