Completed
Push — master ( 6ffec3...6f905d )
by Oleg
06:32
created

AbstractTest::getRelationBody()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 5
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 10
cp 0
crap 12
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Generator\Tests;
5
6
use SlayerBirden\DFCodeGeneration\Generator\GeneratorInterface;
7
8
abstract class AbstractTest implements GeneratorInterface
9
{
10
    /**
11
     * @var EntityProviderFactoryInterface
12
     */
13
    protected $entityProviderFactory;
14
    /**
15
     * @var string
16
     */
17
    protected $entityClassName;
18
    /**
19
     * @var EntityProviderInterface[]
20
     */
21
    protected $providers = [];
22
    private $innerProviders = [];
23
    private $appended = [];
24
25 3
    public function __construct(
26
        string $entityClassName,
27
        EntityProviderFactoryInterface $entityProviderFactory
28
    ) {
29 3
        $this->entityProviderFactory = $entityProviderFactory;
30 3
        $this->entityClassName = $entityClassName;
31 3
    }
32
33
    protected function getLatestProvider()
34
    {
35
        if (count($this->providers)) {
36
            return end($this->providers);
37
        } else {
38
            return $this->entityProviderFactory->create($this->entityClassName);
39
        }
40
    }
41
42 3
    protected function generateHaveInRepo(int $count = 1): string
43
    {
44 3
        $generated = 0;
45 3
        $body = '';
46 3
        while ($generated < $count) {
47 3
            $provider = $this->entityProviderFactory->create($this->entityClassName);
48 3
            $body .= $this->getHaveInRepoPhrase($provider);
49 3
            $this->providers[] = $provider;
50 3
            $generated++;
51
        }
52 3
        return $body;
53
    }
54
55 3
    private function getInnerProvider(string $entity): EntityProviderInterface
56
    {
57 3
        if (!isset($this->innerProviders[$entity])) {
58 3
            $this->innerProviders[$entity] = $this->entityProviderFactory->create($entity);
59
        }
60
61 3
        return $this->innerProviders[$entity];
62
    }
63
64 3
    private function appendOnce(string $content, string $to)
65
    {
66 3
        if (in_array($content, $this->appended, true)) {
67 2
            return $to;
68
        }
69 3
        $this->appended[] = $content;
70 3
        return $to . $content;
71
    }
72
73 3
    private function getHaveInRepoPhrase(EntityProviderInterface $provider): string
74
    {
75 3
        $body = '';
76
77 3
        $params = [];
78 3
        foreach ($provider->getEntitySpec() as $item) {
79 3
            $key = $item['name'];
80 3
            $type = $item['type'];
81 3
            $entity = $item['entity'] ?? '';
82
            switch ($type) {
83 3
                case 'manytoone':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
84 2
                    $innerProvider = $this->getInnerProvider($entity);
85 2
                    $body = $this->appendOnce(
86 2
                        $this->getHaveInRepoPhrase($innerProvider) . $this->getUsagePhrase($innerProvider),
87 2
                        $body
88
                    );
89 2
                    $params[$key] = '$' . $innerProvider->getShortName();
90 2
                    break;
91 3
                case 'manytomany':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
92 1
                    $innerProvider = $this->getInnerProvider($entity);
93 1
                    $body = $this->appendOnce(
94 1
                        $this->getHaveInRepoPhrase($innerProvider) . $this->getUsagePhrase($innerProvider),
95 1
                        $body
96
                    );
97 1
                    $params[$key] = '[$' . $innerProvider->getShortName() . ']';
98 1
                    break;
99 3
                case 'onetoone':
100
                    $innerProvider = $this->entityProviderFactory->create($entity);
101
                    $body .= $this->getHaveInRepoPhrase($innerProvider) . $this->getUsagePhrase($innerProvider);
102
                    $params[$key] = '$' . $innerProvider->getShortName();
103
                    break;
104
                default:
105 3
                    $params[$key] = new ProviderValuePromise($provider, $key);
106 3
                    break;
107
            }
108
        }
109
110 3
        $params = $this->resolvePromises($params);
111
112 3
        $body .= '$I->haveInRepository(%1$s, %2$s);';
113
        $args = [
114 3
            '\'' . $provider->getEntityClassName() . '\'',
115 3
            var_export($params, true),
116
        ];
117 3
        $body = sprintf($body, ...$args);
118 3
        $body = preg_replace("/'(\\[?\\$\w*\\]?)'/", '$1', $body);
119
120 3
        return $body . PHP_EOL;
121
    }
122
123
    private function resolvePromises(array $params): array
124
    {
125 3
        return array_map(function ($item) {
126 3
            if ($item instanceof ProviderValuePromise) {
127 3
                return $item->resolve();
128
            }
129 3
            return $item;
130 3
        }, $params);
131
    }
132
133
    protected function getHaveInRepoParams(int $idx = 0): array
134
    {
135
        if (isset($this->providers[$idx])) {
136
            return $this->providers[$idx]->getPostParams();
137
        }
138
139
        throw new \InvalidArgumentException('Wrong id provided.');
140
    }
141
142 3
    protected function getUsagePhrase(EntityProviderInterface $provider): string
143
    {
144 3
        $body = '$%s = $I->grabEntityFromRepository(%s, [\'id\' => %d]);';
145
146
        $args = [
147 3
            $provider->getShortName(),
148 3
            '\'' . $provider->getEntityClassName() . '\'',
149 3
            $provider->getId(),
150
        ];
151 3
        $body = sprintf($body, ...$args);
152
153 3
        return $body . PHP_EOL;
154
    }
155
}
156