Passed
Pull Request — master (#40)
by Mariano
02:38
created

Phiremock::getFactoryClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 9
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\Extension as CodeceptionExtension;
22
use Mcustiel\Phiremock\Codeception\Extension\Config;
23
use Mcustiel\Phiremock\Codeception\Extension\PhiremockProcessManager;
24
25
class Phiremock extends CodeceptionExtension
26
{
27
    /** @var array */
28
    public static $events = [
29
        'suite.before' => 'startProcess',
30
        'suite.after'  => 'stopProcess',
31
    ];
32
33
    /** @var array */
34
    protected $config = Config::DEFAULT_CONFIG;
35
36
    /** @var PhiremockProcessManager */
37
    private $process;
38
39
    /** @var Config */
40
    private $extensionConfig;
41
42
    public function __construct(
43
        array $config,
44
        array $options,
45
        PhiremockProcessManager $process = null
46
    ) {
47
        $this->setDefaultLogsPath();
48
        parent::__construct($config, $options);
49
        $this->extensionConfig = new Config($this->config);
50
        $this->initProcess($process);
51
    }
52
53
    public function startProcess(): void
54
    {
55
        $this->writeln('Starting default phiremock instance...');
56
        $this->process->start($this->extensionConfig);
57
        foreach ($this->extensionConfig->getExtraInstances() as $configInstance) {
58
            $this->writeln('Starting extra phiremock instance...');
59
            $this->process->start($configInstance);
60
        }
61
        $this->executeDelay();
62
    }
63
64
    public function stopProcess(): void
65
    {
66
        $this->writeln('Stopping phiremock...');
67
        $this->process->stop();
68
    }
69
70
    private function executeDelay(): void
71
    {
72
        $delay = $this->extensionConfig->getDelay();
73
        if ($delay > 0) {
74
            sleep($delay);
75
        }
76
    }
77
78
    private function initProcess(?PhiremockProcessManager $process): void
79
    {
80
        $this->process = $process ?? new PhiremockProcessManager();
81
    }
82
83
    private function setDefaultLogsPath(): void
84
    {
85
        $this->config['logs_path'] = Config::getDefaultLogsPath();
86
    }
87
}
88