Completed
Branch guard_coverage (f2aaf0)
by Pablo
03:07
created

PrePushTool::executeOriginalHook()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 10
rs 9.4285
ccs 6
cts 6
cp 1
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
3
namespace PhpGitHooks\Module\Git\Service;
4
5
use PhpGitHooks\Infrastructure\CommandBus\CommandBus\CommandBus;
6
use PhpGitHooks\Infrastructure\CommandBus\QueryBus\QueryBus;
7
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinderQuery;
8
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse;
9
use PhpGitHooks\Module\Git\Contract\Exception\InvalidPushException;
10
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse;
11
use PhpGitHooks\Module\Git\Contract\Response\GoodJobLogoResponse;
12
use PhpGitHooks\Module\Git\Model\PrePushOriginalExecutorInterface;
13
use PhpGitHooks\Module\PhpUnit\Contract\Command\GuardCoverageCommand;
14
use PhpGitHooks\Module\PhpUnit\Contract\Command\PhpUnitToolCommand;
15
use PhpGitHooks\Module\PhpUnit\Contract\Command\StrictCoverageCommand;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class PrePushTool
19
{
20
    const PRE_PUSH_HOOK = '<comment>Pre-push hook</comment>';
21
    /**
22
     * @var QueryBus
23
     */
24
    private $queryBus;
25
    /**
26
     * @var PrePushOriginalExecutorInterface
27
     */
28
    private $prePushOriginalExecutor;
29
    /**
30
     * @var OutputInterface
31
     */
32
    private $output;
33
    /**
34
     * @var CommandBus
35
     */
36
    private $commandBus;
37
38
    /**
39
     * PrePushTool constructor.
40
     *
41
     * @param QueryBus                         $queryBus
42
     * @param PrePushOriginalExecutorInterface $prePushOriginalExecutor
43
     * @param OutputInterface                  $output
44
     * @param CommandBus                       $commandBus
45
     */
46 3
    public function __construct(
47
        QueryBus $queryBus,
48
        PrePushOriginalExecutorInterface $prePushOriginalExecutor,
49
        OutputInterface $output,
50
        CommandBus $commandBus
51
    ) {
52 3
        $this->queryBus = $queryBus;
53 3
        $this->prePushOriginalExecutor = $prePushOriginalExecutor;
54 3
        $this->output = $output;
55 3
        $this->commandBus = $commandBus;
56 3
    }
57
58
    /**
59
     * @param string $remote
60
     * @param string $url
61
     *
62
     * @throws InvalidPushException
63
     */
64 3
    public function execute($remote, $url)
65
    {
66
        /** @var ConfigurationDataResponse $configurationData */
67 3
        $configurationData = $this->queryBus->handle(new ConfigurationDataFinderQuery());
68 3
        $prePushResponse = $configurationData->getPrePush();
69
70 3
        if (true === $prePushResponse->isPrePush()) {
71 2
            $this->output->writeln(self::PRE_PUSH_HOOK);
72 2
            $this->executeOriginalHook($remote, $url, $prePushResponse->getErrorMessage());
73
74 1
            $phpunitResponse = $prePushResponse->getPhpUnit();
75
76 1 View Code Duplication
            if (true === $phpunitResponse->isPhpunit()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77 1
                $this->commandBus->handle(
78 1
                    new PhpUnitToolCommand(
79 1
                        $phpunitResponse->isPhpunitRandomMode(),
80 1
                        $phpunitResponse->getPhpunitOptions(),
81 1
                        $prePushResponse->getErrorMessage()
82
                    )
83
                );
84
85 1
                $phpunitStrictCoverageResponse = $prePushResponse->getPhpUnitStrictCoverage();
86
87 1
                if (true === $phpunitStrictCoverageResponse->isPhpunitStrictCoverage()) {
88 1
                    $this->commandBus->handle(
89 1
                        new StrictCoverageCommand(
90 1
                            $phpunitStrictCoverageResponse->getMinimum(),
91 1
                            $prePushResponse->getErrorMessage()
92
                        )
93
                    );
94
                }
95
96 1
                $phpunitGuardCoverageResponse = $prePushResponse->getPhpUnitGuardCoverage();
97
98 1
                if (true === $phpunitGuardCoverageResponse->isEnabled()) {
99 1
                    $this->commandBus->handle(
100 1
                        new GuardCoverageCommand($phpunitGuardCoverageResponse->getWarningMessage())
101
                    );
102
                }
103
            }
104
105 1
            $this->output->writeln(GoodJobLogoResponse::paint($prePushResponse->getRightMessage()));
106
        }
107 2
    }
108
109
    /**
110
     * @param string $remote
111
     * @param string $url
112
     * @param string $errorMessage
113
     *
114
     * @throws InvalidPushException
115
     */
116 2
    private function executeOriginalHook($remote, $url, $errorMessage)
117
    {
118 2
        $response = $this->prePushOriginalExecutor->execute($remote, $url);
119
120 2
        if (null != $response) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $response of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
121 1
            $this->output->writeln(BadJobLogoResponse::paint($errorMessage));
122
123 1
            throw new InvalidPushException();
124
        }
125 1
    }
126
}
127