Passed
Pull Request — master (#41)
by Mariano
01:40
created

Phiremock::startProcess()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 1
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of phiremock-codeception-extension.
4
 *
5
 * phiremock-codeception-extension is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * phiremock-codeception-extension is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with phiremock-codeception-extension.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace Codeception\Extension;
20
21
use Codeception\Event\SuiteEvent;
22
use Codeception\Extension as CodeceptionExtension;
23
use Codeception\Suite;
24
use Mcustiel\Phiremock\Codeception\Extension\Config;
25
use Mcustiel\Phiremock\Codeception\Extension\PhiremockProcessManager;
26
use Symfony\Component\EventDispatcher\EventDispatcher;
27
28
class Phiremock extends CodeceptionExtension
29
{
30
    /** @var array */
31
    public static $events = [
32
        'suite.before' => 'startProcess',
33
        'suite.after'  => 'stopProcess',
34
    ];
35
36
    /** @var array */
37
    protected $config = Config::DEFAULT_CONFIG;
38
39
    /** @var PhiremockProcessManager */
40
    private $process;
41
42
    /** @var Config */
43
    private $extensionConfig;
44
45
    public function __construct(
46
        array $config,
47
        array $options,
48
        PhiremockProcessManager $process = null
49
    ) {
50
        $this->setDefaultLogsPath();
51
        parent::__construct($config, $options);
52
        $this->extensionConfig = new Config($this->config, $this->getOutputCallable());
53
        $this->initProcess($process);
54
    }
55
56
    public function startProcess(SuiteEvent $event): void
57
    {
58
        $this->writeln('Starting default phiremock instance...');
59
        $suite = $event->getSuite();
60
        if ($this->mustRunForSuite($suite, $this->extensionConfig->getSuites())) {
61
            $this->process->start($this->extensionConfig);
62
        }
63
        foreach ($this->extensionConfig->getExtraInstances() as $configInstance) {
64
            if ($this->mustRunForSuite($suite, $configInstance->getSuites())) {
65
                $this->writeln('Starting extra phiremock instance...');
66
                $this->process->start($configInstance);
67
            }
68
        }
69
        $this->executeDelay();
70
    }
71
72
    public function stopProcess(): void
73
    {
74
        $this->writeln('Stopping phiremock...');
75
        $this->process->stop();
76
    }
77
78
    public function getOutputCallable(): callable
79
    {
80
        return function (string $message) {
81
            $this->writeln($message);
82
        };
83
    }
84
85
    private function mustRunForSuite(Suite $suite, array $allowedSuites): bool
86
    {
87
        return empty($allowedSuites) || in_array($suite->getBaseName(), $allowedSuites, true);
88
    }
89
90
    private function executeDelay(): void
91
    {
92
        $delay = $this->extensionConfig->getDelay();
93
        if ($delay > 0) {
94
            sleep($delay);
95
        }
96
    }
97
98
    private function initProcess(?PhiremockProcessManager $process): void
99
    {
100
        $this->process = $process ?? new PhiremockProcessManager($this->getOutputCallable());
101
    }
102
103
    private function setDefaultLogsPath(): void
104
    {
105
        if (!isset($this->config['logs_path'])) {
106
            $this->config['logs_path'] = Config::getDefaultLogsPath();
107
        }
108
    }
109
}
110