ProjectTypeResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
eloc 22
c 4
b 0
f 0
dl 0
loc 58
ccs 14
cts 14
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 16 4
1
<?php
2
3
/**
4
 * Copyright MediaCT. All rights reserved.
5
 * https://www.mediact.nl
6
 */
7
8
namespace Mediact\TestingSuite\Composer;
9
10
use Composer\Composer;
11
12
/**
13
 * Resolves the project type.
14
 */
15
class ProjectTypeResolver
16
{
17
    /**
18
     * The key from the composer configuration which contains other configuration.
19
     */
20
    public const COMPOSER_CONFIG_KEY = 'mediact-testing-suite';
21
22
    /**
23
     * The key in the configuration, which determines the overwrite for the type.
24
     */
25
    public const COMPOSER_CONFIG_TYPE_KEY = 'type';
26
27
    /** @var Composer */
28
    private $composer;
29
30
    /** @var array */
31
    private $mapping = [
32
        'magento2-module' => 'magento2',
33
        'magento-module'  => 'magento1',
34
        'magento2-project' => 'magento2',
35
        'magento-project' => 'magento2',
36
        'alumio-project'  => 'alumio',
37
        'laravel-project' => 'laravel',
38
    ];
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param Composer   $composer
44
     * @param array|null $mapping
45
     */
46 5
    public function __construct(Composer $composer, array $mapping = null)
47
    {
48 5
        $this->composer = $composer;
49 5
        $this->mapping  = $mapping ?? $this->mapping;
50 5
    }
51
52
    /**
53
     * Get the type.
54
     *
55
     * @return string
56
     */
57 5
    public function resolve(): string
58
    {
59 5
        $config = $this->composer->getConfig();
60
61 5
        if ($config->has(static::COMPOSER_CONFIG_KEY)) {
62 1
            $configNode = $config->get(static::COMPOSER_CONFIG_KEY);
63 1
            if (isset($configNode[static::COMPOSER_CONFIG_TYPE_KEY])) {
64 1
                return $configNode[static::COMPOSER_CONFIG_TYPE_KEY];
65
            }
66
        }
67
68 4
        $packageType = $this->composer->getPackage()->getType();
69
70 4
        return array_key_exists($packageType, $this->mapping)
71 3
            ? $this->mapping[$packageType]
72 4
            : 'default';
73
    }
74
}
75