ScanConfigurationAction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 28
c 1
b 0
f 0
dl 0
loc 54
ccs 0
cts 18
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A run() 0 34 4
1
<?php
2
3
namespace App\Action;
4
5
use App\Action\AbstractAction;
6
use App\Action\ActionInterface;
7
use App\Action\Context;
8
use App\Facades\Log;
9
use App\Provider\ProviderInterface;
10
use Ronanchilvers\Foundation\Config;
11
use RuntimeException;
12
use Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * Action to scan the project repository for configuration information
16
 *
17
 * @author Ronan Chilvers <[email protected]>
18
 */
19
class ScanConfigurationAction extends AbstractAction
20
{
21
    /**
22
     * @var \App\Provider\ProviderInterface
23
     */
24
    protected $provider;
25
26
    /**
27
     * Class constructor
28
     *
29
     * @author Ronan Chilvers <[email protected]>
30
     */
31
    public function __construct(ProviderInterface $provider)
32
    {
33
        $this->provider = $provider;
34
    }
35
36
    /**
37
     * @see \App\Action\ActionInterface::run()
38
     */
39
    public function run(Config $configuration, Context $context)
40
    {
41
        $project             = $context->getOrThrow('project', 'Invalid or missing project');
42
        $deployment          = $context->getOrThrow('deployment', 'Invalid or missing deployment');
43
        $remoteConfiguration = $this->provider->scanConfiguration(
44
            $project,
45
            $deployment,
46
            function($type, $detail = '') use ($deployment) {
47
                $this->eventFinder->event(
48
                    $type,
49
                    $deployment,
50
                    'Scan Configuration',
51
                    $detail
52
                );
53
            }
54
        );
55
        if (is_null($remoteConfiguration)) {
56
            $deployment->configuration = Yaml::dump($configuration->getAll(), 10);
57
            if (!$deployment->save()) {
58
                throw new RuntimeException('Unable to store configuration data on deployment');
59
            }
60
            return;
61
        }
62
        Log::debug('Merging remote configuration', [
63
            'current' => $configuration->getAll(),
64
            'remote'  => $remoteConfiguration->getAll(),
65
        ]);
66
        $configuration->merge($remoteConfiguration);
67
        Log::debug('Merged configuration', [
68
            'current' => $configuration->getAll(),
69
        ]);
70
        $deployment->configuration = Yaml::dump($configuration->getAll(), 10, 2);
71
        if (!$deployment->save()) {
72
            throw new RuntimeException('Unable to store configuration data on deployment');
73
        }
74
    }
75
}
76