Completed
Push — master ( ff8711...d29de7 )
by Pol
05:47
created

Composer::getComposer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 13
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Services;
6
7
/**
8
 * Parse composer package information.
9
 */
10
final class Composer
11
{
12
    /**
13
     * @var string
14
     */
15
    private $workingDir;
16
17
    /**
18
     * Composer constructor.
19
     *
20
     * @param string $workingDir
21
     */
22 1
    public function __construct($workingDir)
23
    {
24 1
        $this->workingDir = \realpath($workingDir);
25 1
    }
26
27
    /**
28
     * @return mixed
29
     */
30 1
    public function getComposer()
31
    {
32 1
        $composerFile = $this->workingDir . '/composer.json';
33
34 1
        if (!\file_exists($composerFile)) {
35
            return false;
36
        }
37
38 1
        if (false !== $content = \file_get_contents($composerFile)) {
39 1
            return \json_decode($content, true);
40
        }
41
42
        return [];
43
    }
44
45
    /**
46
     * @param string $configKey
47
     *
48
     * @return mixed
49
     */
50 1
    public function getConfig($configKey)
51
    {
52 1
        $composer = $this->getComposer() + ['config' => []];
53
54 1
        return $composer['config'][$configKey] ?? null;
55
    }
56
57 1
    public function getExtra()
58
    {
59 1
        return $this->getComposer()['extra'] ?? [];
60
    }
61
62
    /**
63
     * @return string
64
     */
65 1
    public function getName()
66
    {
67 1
        return $this->hasName() ? $this->getComposer()['name'] : '';
68
    }
69
70
    /**
71
     * @return string
72
     */
73 1
    public function getProject()
74
    {
75 1
        return $this->hasName() ? \explode('/', $this->getName())[1] : '';
76
    }
77
78
    /**
79
     * @return string
80
     */
81 1
    public function getType()
82
    {
83 1
        return $this->hasType() ? $this->getComposer()['type'] : '';
84
    }
85
86
    /**
87
     * @return string
88
     */
89 1
    public function getVendor()
90
    {
91 1
        return $this->hasName() ? \explode('/', $this->getName())[0] : '';
92
    }
93
94
    /**
95
     * @return bool
96
     */
97 1
    public function hasName()
98
    {
99 1
        return isset($this->getComposer()['name']);
100
    }
101
102
    /**
103
     * @return bool
104
     */
105 1
    public function hasType()
106
    {
107 1
        return isset($this->getComposer()->type);
108
    }
109
110
    /**
111
     * @param string $workingDir
112
     *
113
     * @return Composer
114
     */
115 1
    public function setWorkingDir($workingDir)
116
    {
117 1
        $this->workingDir = $workingDir;
118
119 1
        return $this;
120
    }
121
}
122