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
Pull Request — master (#77)
by joseph
15:21
created

SetRelationCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 3
crap 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
7
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class SetRelationCommand extends AbstractCommand
13
{
14
    public const OPT_ENTITY1       = 'entity1';
15
    public const OPT_ENTITY1_SHORT = 'm';
16
17
    public const OPT_HAS_TYPE       = 'hasType';
18
    public const OPT_HAS_TYPE_SHORT = 't';
19
20
    public const OPT_ENTITY2       = 'entity2';
21
    public const OPT_ENTITY2_SHORT = 'i';
22
23
    /**
24
     * @var RelationsGenerator
25
     */
26
    protected $relationsGenerator;
27
28
    /**
29
     * SetRelationCommand constructor.
30
     *
31
     * @param RelationsGenerator $relationsGenerator
32
     * @param NamespaceHelper    $namespaceHelper
33
     * @param null|string        $name
34
     *
35
     * @throws DoctrineStaticMetaException
36
     */
37 3
    public function __construct(
38
        RelationsGenerator $relationsGenerator,
39
        NamespaceHelper $namespaceHelper,
40
        ?string $name = null
41
    ) {
42 3
        parent::__construct($namespaceHelper, $name);
43 3
        $this->relationsGenerator = $relationsGenerator;
44 3
    }
45
46
    /**
47
     * @throws DoctrineStaticMetaException
48
     */
49 3
    public function configure(): void
50
    {
51
        try {
52 3
            $this->setName(AbstractCommand::COMMAND_PREFIX . 'set:relation')
53 3
                 ->setDefinition(
54
                     [
55 3
                         new InputOption(
56 3
                             self::OPT_ENTITY1,
57 3
                             self::OPT_ENTITY1_SHORT,
58 3
                             InputOption::VALUE_REQUIRED,
59 3
                             'First entity in relation'
60
                         ),
61 3
                         new InputOption(
62 3
                             self::OPT_HAS_TYPE,
63 3
                             self::OPT_HAS_TYPE_SHORT,
64 3
                             InputOption::VALUE_REQUIRED,
65
                             'What type of relation is it? '
66 3
                             . 'Must be one of ' . RelationsGenerator::class . '::RELATION_TYPES'
67
                         ),
68 3
                         new InputOption(
69 3
                             self::OPT_ENTITY2,
70 3
                             self::OPT_ENTITY2_SHORT,
71 3
                             InputOption::VALUE_REQUIRED,
72 3
                             'Second entity in relation'
73
                         ),
74 3
                         $this->getProjectRootPathOption(),
75 3
                         $this->getProjectRootNamespaceOption(),
76 3
                         $this->getSrcSubfolderOption(),
77
                     ]
78 3
                 )->setDescription(
79
                     'Set a relation between 2 entities. The relation must be one of '
80 3
                     . RelationsGenerator::class . '::RELATION_TYPES'
81
                 );
82
        } catch (\Exception $e) {
83
            throw new DoctrineStaticMetaException(
84
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
85
                $e->getCode(),
86
                $e
87
            );
88
        }
89 3
    }
90
91
    /**
92
     * @param InputInterface  $input
93
     * @param OutputInterface $output
94
     *
95
     * @return int|null|void
96
     * @throws DoctrineStaticMetaException
97
     */
98 2
    public function execute(InputInterface $input, OutputInterface $output)
99
    {
100
        try {
101 2
            $output->writeln(
102
                '<comment>Setting relation: '
103 2
                . $input->getOption(static::OPT_ENTITY1)
104 2
                . ' ' . $input->getOption(static::OPT_HAS_TYPE)
105 2
                . ' ' . $input->getOption(static::OPT_ENTITY2)
106 2
                . '</comment>'
107
            );
108 2
            $this->checkOptions($input);
109 2
            $hasType = $input->getOption(static::OPT_HAS_TYPE);
110 2
            if (!\in_array($hasType, RelationsGenerator::HAS_TYPES, true)) {
111 1
                $hasType = RelationsGenerator::PREFIX_OWNING . $hasType;
112 1
                if (!\in_array($hasType, RelationsGenerator::HAS_TYPES, true)) {
113
                    throw new DoctrineStaticMetaException(
114
                        'Invalid hasType ' . $input->getOption(static::OPT_HAS_TYPE)
115
                        . ' Must be one of ' . print_r(RelationsGenerator::HAS_TYPES, true)
116
                    );
117
                }
118
            }
119 2
            $this->relationsGenerator
120 2
                ->setPathToProjectRoot($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_PATH))
121 2
                ->setProjectRootNamespace($input->getOption(AbstractCommand::OPT_PROJECT_ROOT_NAMESPACE));
122
123 2
            $this->relationsGenerator->setEntityHasRelationToEntity(
124 2
                $input->getOption(static::OPT_ENTITY1),
125 2
                $hasType,
126 2
                $input->getOption(static::OPT_ENTITY2)
127
            );
128 2
            $output->writeln('<info>completed</info>');
129
        } catch (\Exception $e) {
130
            throw new DoctrineStaticMetaException(
131
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
132
                $e->getCode(),
133
                $e
134
            );
135
        }
136 2
    }
137
}
138