Installer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Gameap\Services\Modules;
4
5
use Gameap\Adapters\Archiver;
6
use Gameap\Exceptions\GameapException;
7
use Illuminate\Support\Facades\Config;
8
9
class Installer
10
{
11
    private const CONFIG_MODULES_PATH_NAME = 'modules.paths.modules';
12
13
    /** @var MarketplaceService */
14
    private $marketplaceService;
15
16
    /** @var Archiver */
17
    private $archiver;
18
19
    public function __construct(MarketplaceService $marketplaceService, Archiver $archiver)
20
    {
21
        $this->marketplaceService = $marketplaceService;
22
        $this->archiver           = $archiver;
23
    }
24
25
    /**
26
     * @throws GameapException
27
     * @throws \Gameap\Exceptions\InvalidArgumentValueException
28
     * @throws \splitbrain\PHPArchive\ArchiveCorruptedException
29
     * @throws \splitbrain\PHPArchive\ArchiveIOException
30
     * @throws \splitbrain\PHPArchive\ArchiveIllegalCompressionException
31
     */
32
    public function install(string $moduleID, string $version = ''): void
33
    {
34
        $modulesPath     = Config::get(self::CONFIG_MODULES_PATH_NAME);
35
        $destinationPath = $modulesPath . '/' . ucfirst($moduleID);
36
37
        if (!is_writable($modulesPath)) {
38
            throw new GameapException('No write permissions to modules directory');
39
        }
40
41
        $downloadedFilePath = $this->marketplaceService->downloadModule($moduleID, $version);
42
        $this->archiver->extractTarGzip($downloadedFilePath, $destinationPath, 1);
43
    }
44
}
45