GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 75bdf9...8faa57 )
by joseph
83:56 queued 81:04
created

CreateConstraintAction::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\ConstraintCreator;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\ConstraintValidatorCreator;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\Config;
9
10
class CreateConstraintAction implements ActionInterface
11
{
12
    private const SUFFIX_CONSTRAINT           = 'Constraint';
13
    private const SUFFIX_CONSTRAINT_VALIDATOR = 'ConstraintValidator';
14
15
    /**
16
     * @var ConstraintCreator
17
     */
18
    protected $constraintCreator;
19
    /**
20
     * @var ConstraintValidatorCreator
21
     */
22
    protected $constraintValidatorCreator;
23
24
    /**
25
     * @var string
26
     */
27
    private $constraintsRootNamespace;
28
29
    /**
30
     * @var string
31
     */
32
    private $constraintShortName;
33
34 2
    public function __construct(
35
        ConstraintCreator $constraintCreator,
36
        ConstraintValidatorCreator $constraintValidatorCreator,
37
        NamespaceHelper $namespaceHelper,
38
        Config $config
39
    ) {
40 2
        $this->constraintCreator          = $constraintCreator;
41 2
        $this->constraintValidatorCreator = $constraintValidatorCreator;
42 2
        $this->setProjectRootNamespace($namespaceHelper->getProjectRootNamespaceFromComposerJson());
43 2
        $this->setProjectRootDirectory($config::getProjectRootDirectory());
44 2
    }
45
46 2
    public function setProjectRootNamespace(string $projectRootNamespace): self
47
    {
48 2
        $this->constraintCreator->setProjectRootNamespace($projectRootNamespace);
49 2
        $this->constraintValidatorCreator->setProjectRootNamespace($projectRootNamespace);
50 2
        $this->constraintsRootNamespace = $projectRootNamespace . '\\Validation\\Constraints';
51
52 2
        return $this;
53
    }
54
55 2
    public function setProjectRootDirectory(string $projectRootDirectory): self
56
    {
57 2
        $this->constraintCreator->setProjectRootDirectory($projectRootDirectory);
58 2
        $this->constraintValidatorCreator->setProjectRootDirectory($projectRootDirectory);
59
60 2
        return $this;
61
    }
62
63
    /**
64
     * @param string $constraintShortName
65
     *
66
     * @return CreateConstraintAction
67
     */
68 2
    public function setConstraintShortName(string $constraintShortName): self
69
    {
70 2
        $this->constraintShortName = $constraintShortName;
71
72 2
        return $this;
73
    }
74
75 2
    public function run(): void
76
    {
77 2
        if (null === $this->constraintShortName) {
78
            throw new \RuntimeException('You must call setContraintShortname before calling run');
79
        }
80 2
        $this->createConstraint($this->constraintShortName);
81 2
        $this->createConstraintValidator($this->constraintShortName);
82 2
    }
83
84 2
    private function createConstraint(string $constraintShortName): void
85
    {
86 2
        $constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName)
87 2
                         . self::SUFFIX_CONSTRAINT;
88 2
        $this->constraintCreator->createTargetFileObject($constraintFqn)->write();
89 2
    }
90
91 2
    private function stripSuffix(string $constraintShortName): string
92
    {
93 2
        if (false === \ts\stringContains($constraintShortName, self::SUFFIX_CONSTRAINT)) {
94 1
            return $constraintShortName;
95
        }
96
97 1
        $pos = \ts\strpos($constraintShortName, self::SUFFIX_CONSTRAINT);
98
99 1
        return substr($constraintShortName, 0, $pos);
100
    }
101
102 2
    private function createConstraintValidator(string $constraintShortName): void
103
    {
104 2
        $constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) .
105 2
                                  self::SUFFIX_CONSTRAINT_VALIDATOR;
106 2
        $this->constraintCreator->createTargetFileObject($constraintValidatorFqn)->write();
107 2
    }
108
}
109