Passed
Push — feature/unit-tests ( 84c996...7c5ff9 )
by Daniel
05:24
created

prependDoctrineConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\DependencyInjection;
15
16
use Exception;
17
use RuntimeException;
18
use Silverback\ApiComponentBundle\Doctrine\Extension\TablePrefixExtension;
19
use Silverback\ApiComponentBundle\Entity\Core\ComponentInterface;
20
use Silverback\ApiComponentBundle\EventListener\Doctrine\UserListener;
21
use Silverback\ApiComponentBundle\Factory\UserFactory;
22
use Silverback\ApiComponentBundle\Form\FormTypeInterface;
23
use Silverback\ApiComponentBundle\Form\Type\User\ChangePasswordType;
24
use Silverback\ApiComponentBundle\Form\Type\User\NewEmailAddressType;
25
use Silverback\ApiComponentBundle\Form\Type\User\UserRegisterType;
26
use Silverback\ApiComponentBundle\Mailer\UserMailer;
27
use Silverback\ApiComponentBundle\Manager\User\PasswordManager;
28
use Silverback\ApiComponentBundle\Repository\User\UserRepository;
29
use Silverback\ApiComponentBundle\Security\TokenAuthenticator;
30
use Silverback\ApiComponentBundle\Security\UserChecker;
31
use Symfony\Component\Config\FileLocator;
32
use Symfony\Component\DependencyInjection\ContainerBuilder;
33
use Symfony\Component\DependencyInjection\Extension\Extension;
34
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
35
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
36
37
/**
38
 * @author Daniel West <[email protected]>
39
 */
40
class SilverbackApiComponentExtension extends Extension implements PrependExtensionInterface
41
{
42
    /**
43
     * @throws Exception
44
     */
45 1
    public function load(array $configs, ContainerBuilder $container): void
46
    {
47 1
        $configuration = new Configuration();
48 1
        $config = $this->processConfiguration($configuration, $configs);
49
50 1
        $this->loadServiceConfig($container);
51
52 1
        $repeatTtl = $config['user']['password_reset']['repeat_ttl_seconds'];
53 1
        $timeoutSeconds = $config['user']['password_reset']['request_timeout_seconds'];
54 1
        $userClass = $config['user']['class_name'];
55
56 1
        $definition = $container->getDefinition(ChangePasswordType::class);
57 1
        $definition->setArgument('$userClass', $userClass);
58
59 1
        $definition = $container->getDefinition(NewEmailAddressType::class);
60 1
        $definition->setArgument('$userClass', $userClass);
61
62 1
        $definition = $container->getDefinition(PasswordManager::class);
63 1
        $definition->setArgument('$tokenTtl', $repeatTtl);
64
65 1
        $definition = $container->getDefinition(TablePrefixExtension::class);
66 1
        $definition->setArgument('$prefix', $config['table_prefix']);
67
68 1
        $definition = $container->getDefinition(TokenAuthenticator::class);
69 1
        $definition->setArgument('$tokens', $config['security']['tokens']);
70
71 1
        $definition = $container->getDefinition(UserChecker::class);
72 1
        $definition->setArgument('$denyUnverifiedLogin', $config['user']['email_verification']['deny_unverified_login']);
73
74 1
        $definition = $container->getDefinition(UserFactory::class);
75 1
        $definition->setArgument('$userClass', $userClass);
76
77 1
        $definition = $container->getDefinition(UserListener::class);
78 1
        $definition->setArgument('$initialEmailVerifiedState', $config['user']['email_verification']['default']);
79 1
        $definition->setArgument('$verifyEmailOnRegister', $config['user']['email_verification']['verify_on_register']);
80
81 1
        $definition = $container->getDefinition(UserMailer::class);
82 1
        $definition->setArgument('$websiteName', $config['website_name']);
83 1
        $definition->setArgument('$defaultPasswordResetPath', $config['user']['password_reset']['default_reset_path']);
84 1
        $definition->setArgument('$defaultChangeEmailVerifyPath', $config['user']['change_email_address']['default_verify_path']);
85 1
        $definition->setArgument('$sendUserWelcomeEmailEnabled', $config['user']['emails']['user_welcome']);
86 1
        $definition->setArgument('$sendUserEnabledEmailEnabled', $config['user']['emails']['user_enabled']);
87 1
        $definition->setArgument('$sendUserUsernameChangedEmailEnabled', $config['user']['emails']['user_username_changed']);
88 1
        $definition->setArgument('$sendUserPasswordChangedEmailEnabled', $config['user']['emails']['user_password_changed']);
89
90 1
        $definition = $container->getDefinition(UserRegisterType::class);
91 1
        $definition->setArgument('$userClass', $userClass);
92
93 1
        $definition = $container->getDefinition(UserRepository::class);
94 1
        $definition->setArgument('$passwordRequestTimeout', $timeoutSeconds);
95 1
        $definition->setArgument('$entityClass', $userClass);
96 1
    }
97
98
    /**
99
     * @throws Exception
100
     */
101 1
    private function loadServiceConfig(ContainerBuilder $container): void
102
    {
103 1
        $container->registerForAutoconfiguration(FormTypeInterface::class)
104 1
            ->addTag('silverback_api_component.form_type');
105
106 1
        $container->registerForAutoconfiguration(ComponentInterface::class)
107 1
            ->addTag('silverback_api_component.entity.component');
108
109 1
        $loader = new PhpFileLoader(
110 1
            $container,
111 1
            new FileLocator(__DIR__ . '/../Resources/config')
112
        );
113 1
        $loader->load('services.php');
114 1
    }
115
116 1
    public function prepend(ContainerBuilder $container): void
117
    {
118 1
        $configs = $container->getExtensionConfig($this->getAlias());
119 1
        $configuration = new Configuration();
120 1
        $config = $this->processConfiguration($configuration, $configs);
121 1
        $srcBase = __DIR__ . '/..';
122 1
        $configBasePath = $srcBase . '/Resources/config/api_platform';
123 1
        $mappingPaths = [$srcBase . '/Entity/Core'];
124 1
        foreach ($config['enabled_components'] as $key => $enabled_component) {
125 1
            if (true === $enabled_component) {
126 1
                $mappingPaths[] = sprintf('%s/%s.yaml', $configBasePath, $key);
127
            }
128
        }
129 1
        $websiteName = $config['website_name'];
130 1
        $container->prependExtensionConfig(
131 1
            'api_platform',
132
            [
133 1
                'title' => $websiteName,
134 1
                'description' => sprintf('API for %s', $websiteName),
135
                'collection' => [
136
                    'pagination' => [
137
                        'client_items_per_page' => true,
138
                        'items_per_page_parameter_name' => 'perPage',
139
                        'maximum_items_per_page' => 100,
140
                    ],
141
                ],
142
                'mapping' => [
143 1
                    'paths' => $mappingPaths,
144
                ],
145
                'swagger' => [
146
                    'api_keys' => [
147
                        'API Token' => [
148
                            'name' => 'X-AUTH-TOKEN',
149
                            'type' => 'header',
150
                        ],
151
                        'JWT (use prefix `Bearer`)' => [
152
                            'name' => 'Authorization',
153
                            'type' => 'header',
154
                        ],
155
                    ],
156
                ],
157
            ]
158
        );
159
160 1
        $bundles = $container->getParameter('kernel.bundles');
161 1
        if (isset($bundles['DoctrineBundle'])) {
162 1
            $this->prependDoctrineConfig($container);
163
        }
164 1
        if (isset($bundles['LiipImagineBundle'])) {
165 1
            $this->prependLiipConfig($container);
166
        }
167 1
    }
168
169 1
    private function prependDoctrineConfig(ContainerBuilder $container)
170
    {
171 1
        $container->prependExtensionConfig(
172 1
            'doctrine',
173
            [
174
                //                'orm' => [
175
                //                    'filters' => [
176
                //                        'publishable' => [
177
                //                            'class' => PublishableFilter::class,
178
                //                            'enabled' => false
179
                //                        ]
180
                //                    ]
181
                //                ]
182 1
            ]
183
        );
184 1
    }
185
186 1
    private function prependLiipConfig(ContainerBuilder $container)
187
    {
188 1
        $projectDir = $container->getParameter('kernel.project_dir');
189 1
        $uploadsDir = $projectDir . '/var/uploads';
190 1
        if (!@mkdir($uploadsDir) && !is_dir($uploadsDir)) {
191
            throw new RuntimeException(sprintf('Directory "%s" was not created', $uploadsDir));
192
        }
193 1
        $container->prependExtensionConfig(
194 1
            'liip_imagine',
195
            [
196
                'loaders' => [
197
                    'default' => [
198
                        'filesystem' => [
199
                            'data_root' => [
200 1
                                'uploads' => $uploadsDir,
201 1
                                'default' => $projectDir . '/public',
202
                            ],
203
                        ],
204
                    ],
205
                ],
206
                'filter_sets' => [
207
                    'placeholder_square' => [
208
                        'jpeg_quality' => 10,
209
                        'png_compression_level' => 9,
210
                        'filters' => [
211
                            'thumbnail' => [
212
                                'size' => [80, 80],
213
                                'mode' => 'outbound',
214
                            ],
215
                        ],
216
                    ],
217
                    'placeholder' => [
218
                        'jpeg_quality' => 10,
219
                        'png_compression_level' => 9,
220
                        'filters' => [
221
                            'thumbnail' => [
222
                                'size' => [100, 100],
223
                                'mode' => 'inset',
224
                            ],
225
                        ],
226
                    ],
227
                    'thumbnail' => [
228
                        'jpeg_quality' => 100,
229
                        'png_compression_level' => 0,
230
                        'filters' => [
231
                            'upscale' => [
232
                                'min' => [636, 636],
233
                            ],
234
                            'thumbnail' => [
235
                                'size' => [636, 636],
236
                                'mode' => 'inset',
237
                                'allow_upscale' => true,
238
                            ],
239
                        ],
240
                    ],
241
                ],
242
            ]
243
        );
244 1
    }
245
}
246