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 ( a4bf8a...e84c94 )
by joseph
14s queued 10s
created

EnumFieldTrait::getEnum()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String;
4
5
// phpcs:disable
6
7
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\EnumFieldInterface;
9
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface;
10
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
11
use Symfony\Component\Validator\Constraints\Choice;
12
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData;
13
14
// phpcs:enable
15
trait EnumFieldTrait
16
{
17
18
    /**
19
     * @var string|null
20
     */
21
    private $enum;
22
23
    /**
24
     * @SuppressWarnings(PHPMD.StaticAccess)
25
     */
26 1
    public static function metaForEnum(ClassMetadataBuilder $builder)
27
    {
28 1
        MappingHelper::setSimpleStringFields(
29 1
            [EnumFieldInterface::PROP_ENUM],
30 1
            $builder,
31 1
            EnumFieldInterface::DEFAULT_ENUM,
32 1
            false
33
        );
34 1
    }
35
36
    /**
37
     * This method sets the validation for this field.
38
     *
39
     * You should add in as many relevant property constraints as you see fit.
40
     *
41
     * @param ValidatorClassMetaData $metadata
42
     *
43
     * @throws \Symfony\Component\Validator\Exception\MissingOptionsException
44
     * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException
45
     * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException
46
     */
47 2
    protected static function validatorMetaForEnum(ValidatorClassMetaData $metadata)
48
    {
49 2
        $metadata->addPropertyConstraint(
50 2
            EnumFieldInterface::PROP_ENUM,
51 2
            new Choice(EnumFieldInterface::ENUM_OPTIONS)
52
        );
53 2
    }
54
55
    /**
56
     * @return string|null
57
     */
58 2
    public function getEnum(): string
59
    {
60 2
        if (null === $this->enum) {
61 1
            return EnumFieldInterface::DEFAULT_ENUM;
62
        }
63
64 2
        return $this->enum;
65
    }
66
67
    /**
68
     * Uses the Symfony Validator and fails back to basic in_array validation with exception
69
     *
70
     * @param string|null $enum
71
     *
72
     * @return self
73
     */
74 2
    public function setEnum(string $enum): self
75
    {
76 2
        $this->enum = $enum;
77 2
        if ($this instanceof ValidatedEntityInterface) {
78 2
            $this->validateProperty(EnumFieldInterface::PROP_ENUM);
79
        } elseif (!\in_array($enum, EnumFieldInterface::ENUM_OPTIONS, true)) {
80
            throw new \InvalidArgumentException(
81
                'Invalid $enum '.$enum.', should be one of :'
82
                .print_r(EnumFieldInterface::ENUM_OPTIONS, true)
83
            );
84
        }
85
86 2
        return $this;
87
    }
88
}
89