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 ( 0b4676...ecfcbd )
by joseph
17s queued 14s
created

CreateConstraintAction   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 154
ccs 0
cts 91
cp 0
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A createEntityConstraintValidator() 0 5 1
A createPropertyConstraint() 0 5 1
A setProjectRootDirectory() 0 6 1
A createPropertyConstraintValidator() 0 5 1
A run() 0 19 4
A stripSuffix() 0 9 2
A createEntityConstraint() 0 5 1
A setProjectRootNamespace() 0 7 1
A setConstraintShortName() 0 5 1
A setPropertyOrEntity() 0 10 2
A __construct() 0 14 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action;
4
// phpcs:disable
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintCreator;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintValidatorCreator;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintCreator;
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintValidatorCreator;
9
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
10
use EdmondsCommerce\DoctrineStaticMeta\Config;
11
// phpcs:enable
12
class CreateConstraintAction implements ActionInterface
13
{
14
    private const SUFFIX_CONSTRAINT           = 'Constraint';
15
    private const SUFFIX_CONSTRAINT_VALIDATOR = 'ConstraintValidator';
16
17
    public const OPTION_PROPERTY = 'property';
18
    public const OPTION_ENTITY   = 'entity';
19
20
    /**
21
     * @var PropertyConstraintCreator
22
     */
23
    protected $propertyConstraintCreator;
24
    /**
25
     * @var PropertyConstraintValidatorCreator
26
     */
27
    protected $propertyConstraintValidatorCreator;
28
29
    /**
30
     * @var string
31
     */
32
    private $constraintsRootNamespace;
33
34
    /**
35
     * @var string
36
     */
37
    private $constraintShortName;
38
    /**
39
     * @var string
40
     */
41
    private $propertyOrEntity = self::OPTION_PROPERTY;
42
    /**
43
     * @var EntityIsValidConstraintCreator
44
     */
45
    private $entityIsValidConstraintCreator;
46
    /**
47
     * @var EntityIsValidConstraintValidatorCreator
48
     */
49
    private $entityIsValidConstraintValidatorCreator;
50
51
    public function __construct(
52
        PropertyConstraintCreator $constraintCreator,
53
        PropertyConstraintValidatorCreator $constraintValidatorCreator,
54
        EntityIsValidConstraintCreator $entityIsValidConstraintCreator,
55
        EntityIsValidConstraintValidatorCreator $entityIsValidConstraintValidatorCreator,
56
        NamespaceHelper $namespaceHelper,
57
        Config $config
58
    ) {
59
        $this->propertyConstraintCreator               = $constraintCreator;
60
        $this->propertyConstraintValidatorCreator      = $constraintValidatorCreator;
61
        $this->entityIsValidConstraintCreator          = $entityIsValidConstraintCreator;
62
        $this->entityIsValidConstraintValidatorCreator = $entityIsValidConstraintValidatorCreator;
63
        $this->setProjectRootNamespace($namespaceHelper->getProjectRootNamespaceFromComposerJson());
64
        $this->setProjectRootDirectory($config::getProjectRootDirectory());
65
    }
66
67
    public function setProjectRootNamespace(string $projectRootNamespace): self
68
    {
69
        $this->propertyConstraintCreator->setProjectRootNamespace($projectRootNamespace);
70
        $this->propertyConstraintValidatorCreator->setProjectRootNamespace($projectRootNamespace);
71
        $this->constraintsRootNamespace = $projectRootNamespace . '\\Validation\\Constraints';
72
73
        return $this;
74
    }
75
76
    public function setProjectRootDirectory(string $projectRootDirectory): self
77
    {
78
        $this->propertyConstraintCreator->setProjectRootDirectory($projectRootDirectory);
79
        $this->propertyConstraintValidatorCreator->setProjectRootDirectory($projectRootDirectory);
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param string $constraintShortName
86
     *
87
     * @return CreateConstraintAction
88
     */
89
    public function setConstraintShortName(string $constraintShortName): self
90
    {
91
        $this->constraintShortName = $constraintShortName;
92
93
        return $this;
94
    }
95
96
    public function setPropertyOrEntity(string $propertyOrEntity): self
97
    {
98
        if (false === \in_array($propertyOrEntity, [self::OPTION_PROPERTY, self::OPTION_ENTITY], true)) {
99
            throw new \InvalidArgumentException(
100
                '$propertyOrEntity must be one of self::OPTION_PROPERTY,self::OPTION_ENTITY'
101
            );
102
        }
103
        $this->propertyOrEntity = $propertyOrEntity;
104
105
        return $this;
106
    }
107
108
    public function run(): void
109
    {
110
        if (null === $this->constraintShortName) {
111
            throw new \RuntimeException('You must call setContraintShortname before calling run');
112
        }
113
        if (self::OPTION_PROPERTY === $this->propertyOrEntity) {
114
            $this->createPropertyConstraint($this->constraintShortName);
115
            $this->createPropertyConstraintValidator($this->constraintShortName);
116
117
            return;
118
        }
119
        if (self::OPTION_ENTITY === $this->propertyOrEntity) {
120
            $this->createEntityConstraint($this->constraintShortName);
121
            $this->createEntityConstraintValidator($this->constraintShortName);
122
123
            return;
124
        }
125
126
        throw new \LogicException('Invalid propertyOrEntity ' . $this->propertyOrEntity);
127
    }
128
129
    private function createPropertyConstraint(string $constraintShortName): void
130
    {
131
        $constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName)
132
                         . self::SUFFIX_CONSTRAINT;
133
        $this->propertyConstraintCreator->createTargetFileObject($constraintFqn)->write();
134
    }
135
136
    private function stripSuffix(string $constraintShortName): string
137
    {
138
        if (false === \ts\stringContains($constraintShortName, self::SUFFIX_CONSTRAINT)) {
0 ignored issues
show
introduced by
The condition false === stringContains...elf::SUFFIX_CONSTRAINT) is always false.
Loading history...
139
            return $constraintShortName;
140
        }
141
142
        $pos = \ts\strpos($constraintShortName, self::SUFFIX_CONSTRAINT);
143
144
        return substr($constraintShortName, 0, $pos);
145
    }
146
147
    private function createPropertyConstraintValidator(string $constraintShortName): void
148
    {
149
        $constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) .
150
                                  self::SUFFIX_CONSTRAINT_VALIDATOR;
151
        $this->propertyConstraintValidatorCreator->createTargetFileObject($constraintValidatorFqn)->write();
152
    }
153
154
    private function createEntityConstraint(string $constraintShortName): void
155
    {
156
        $constraintFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName)
157
                         . self::SUFFIX_CONSTRAINT;
158
        $this->entityIsValidConstraintCreator->createTargetFileObject($constraintFqn)->write();
159
    }
160
161
    private function createEntityConstraintValidator(string $constraintShortName): void
162
    {
163
        $constraintValidatorFqn = $this->constraintsRootNamespace . '\\' . $this->stripSuffix($constraintShortName) .
164
                                  self::SUFFIX_CONSTRAINT_VALIDATOR;
165
        $this->entityIsValidConstraintValidatorCreator->createTargetFileObject($constraintValidatorFqn)->write();
166
    }
167
}
168