Passed
Push — master ( e66d23...e16cc9 )
by Mariano
02:32 queued 01:03
created

Phiremock::initProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
27
class Phiremock extends CodeceptionExtension
28
{
29
    /** @var array */
30
    public static $events = [
31
        'suite.before' => 'startProcess',
32
        'suite.after'  => 'stopProcess',
33
    ];
34
35
    /** @var array */
36
    protected $config = Config::DEFAULT_CONFIG;
37
38
    /** @var PhiremockProcessManager */
39
    private $process;
40
41
    /** @var Config */
42
    private $extensionConfig;
43
44
    public function __construct(
45
        array $config,
46
        array $options,
47
        PhiremockProcessManager $process = null
48
    ) {
49
        $this->setDefaultLogsPath();
50
        parent::__construct($config, $options);
51
        $this->extensionConfig = new Config($this->config, $this->getOutputCallable());
52
        $this->initProcess($process);
53
    }
54
55
    public function startProcess(SuiteEvent $event): void
56
    {
57
        $this->writeln('Starting default phiremock instance...');
58
        $suite = $event->getSuite();
59
        if ($this->mustRunForSuite($suite, $this->extensionConfig->getSuites())) {
60
            $this->process->start($this->extensionConfig);
61
        }
62
        foreach ($this->extensionConfig->getExtraInstances() as $configInstance) {
63
            if ($this->mustRunForSuite($suite, $configInstance->getSuites())) {
64
                $this->writeln('Starting extra phiremock instance...');
65
                $this->process->start($configInstance);
66
            }
67
        }
68
        $this->executeDelay();
69
    }
70
71
    public function stopProcess(): void
72
    {
73
        $this->writeln('Stopping phiremock...');
74
        $this->process->stop();
75
    }
76
77
    public function getOutputCallable(): callable
78
    {
79
        return function (string $message) {
80
            $this->writeln($message);
81
        };
82
    }
83
84
    private function mustRunForSuite(Suite $suite, array $allowedSuites): bool
85
    {
86
        return empty($allowedSuites) || in_array($suite->getBaseName(), $allowedSuites, true);
87
    }
88
89
    private function executeDelay(): void
90
    {
91
        $delay = $this->extensionConfig->getDelay();
92
        if ($delay > 0) {
93
            sleep($delay);
94
        }
95
    }
96
97
    private function initProcess(?PhiremockProcessManager $process): void
98
    {
99
        $this->process = $process ?? new PhiremockProcessManager($this->getOutputCallable());
100
    }
101
102
    private function setDefaultLogsPath(): void
103
    {
104
        if (!isset($this->config['logs_path'])) {
105
            $this->config['logs_path'] = Config::getDefaultLogsPath();
106
        }
107
    }
108
}
109