Completed
Push — master ( 023fff...9c4cb5 )
by Philip
06:35
created

ServiceProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 96.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 11
dl 0
loc 73
ccs 30
cts 31
cp 0.9677
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initMissingServiceProviders() 0 19 4
A boot() 0 6 1
C register() 0 22 7
1
<?php
2
3
/*
4
 * This file is part of the CRUDlex package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[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
namespace CRUDlex\Silex;
13
14
use CRUDlex\EntityDefinitionFactory;
15
use CRUDlex\EntityDefinitionValidator;
16
use CRUDlex\Service;
17
use League\Flysystem\Adapter\Local;
18
use League\Flysystem\Filesystem;
19
use Pimple\Container;
20
use Pimple\ServiceProviderInterface;
21
use Silex\Api\BootableProviderInterface;
22
use Silex\Application;
23
use Silex\Provider\LocaleServiceProvider;
24
use Silex\Provider\SessionServiceProvider;
25
use Silex\Provider\TranslationServiceProvider;
26
use Silex\Provider\TwigServiceProvider;
27
28
/**
29
 * The ServiceProvider setups and initializes the service for Silex.
30
 */
31
class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface
32
{
33
34
    /**
35
     * Initializes needed but yet missing service providers.
36
     *
37
     * @param Container $app
38
     * the application container
39
     */
40 12
    protected function initMissingServiceProviders(Container $app)
41
    {
42
43 12
        if (!$app->offsetExists('translator')) {
44 2
            $app->register(new LocaleServiceProvider());
45 2
            $app->register(new TranslationServiceProvider(), [
46 2
                'locale_fallbacks' => ['en'],
47
            ]);
48
        }
49
50 12
        if (!$app->offsetExists('session')) {
51 2
            $app->register(new SessionServiceProvider());
52
        }
53
54 12
        if (!$app->offsetExists('twig')) {
55 2
            $app->register(new TwigServiceProvider());
56
        }
57 12
        $app['twig.loader.filesystem']->addPath(__DIR__.'/../../views/', 'crud');
58 12
    }
59
60
    /**
61
     * Implements ServiceProviderInterface::register() registering $app['crud'].
62
     * $app['crud'] contains an instance of the ServiceProvider afterwards.
63
     *
64
     * @param Container $app
65
     * the Container instance of the Silex application
66
     */
67 12
    public function register(Container $app)
68
    {
69 12
        if (!$app->offsetExists('crud.filesystem')) {
70 12
            $app['crud.filesystem'] = new Filesystem(new Local(getcwd()));
71
        }
72
73 12
        $doValidate = !$app->offsetExists('crud.validateentitydefinition') || $app['crud.validateentitydefinition'] === true;
74 12
        $validator  = null;
75 12
        if ($doValidate) {
76 12
            $validator = $app->offsetExists('crud.entitydefinitionvalidator')
77
                ? $app['crud.entitydefinitionvalidator']
78 12
                : new EntityDefinitionValidator();
79
        }
80
81
        $app['crud'] = function() use ($app, $validator) {
82 12
            $crudFileCachingDirectory = $app->offsetExists('crud.filecachingdirectory') ? $app['crud.filecachingdirectory'] : null;
83 12
            $entityDefinitionFactory  = $app->offsetExists('crud.entitydefinitionfactory') ? $app['crud.entitydefinitionfactory'] : new EntityDefinitionFactory();
84 12
            $result                   = new Service($app['crud.file'], $crudFileCachingDirectory, $app['url_generator'], $app['translator'], $app['crud.datafactory'], $entityDefinitionFactory, $app['crud.filesystem'], $validator);
85 11
            $result->setTemplate('layout', '@crud/layout.twig');
86 11
            return $result;
87
        };
88 12
    }
89
90
    /**
91
     * Initializes the crud service right after boot.
92
     *
93
     * @param Application $app
94
     * the Container instance of the Silex application
95
     */
96 12
    public function boot(Application $app)
97
    {
98 12
        $this->initMissingServiceProviders($app);
99 12
        $twigSetup = new TwigSetup();
100 12
        $twigSetup->registerTwigExtensions($app);
101 12
    }
102
103
}
104