Completed
Push — master ( 905306...b5ba41 )
by Philip
08:53
created

ServiceProvider::getTemplate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 3
crap 3
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;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
75 12
        if ($doValidate) {
76 12
            $validator = $app->offsetExists('crud.entitydefinitionvalidator')
77
                ? $app['crud.entitydefinitionvalidator']
78 12
                : new EntityDefinitionValidator();
79
        }
80
81 12
        $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();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
84 12
            $result = new Service($app['crud.file'], $crudFileCachingDirectory, $app['url_generator'], $app['translator'], $app['crud.datafactory'], $entityDefinitionFactory, $app['crud.filesystem'], $validator);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 19 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
85
            $result->setTemplate('layout', '@crud/layout.twig');
86
            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