Completed
Push — master ( 9c1f83...0c1135 )
by Paulo Rodrigues
10:00
created

ExtensionHelper/BaseExtensionHelper.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Rj\FrontendBundle\DependencyInjection\ExtensionHelper;
4
5
use Rj\FrontendBundle\Util\Util;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\DefinitionDecorator;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
abstract class BaseExtensionHelper
11
{
12
    private $alias;
13
    protected $container;
14
15
    abstract public function getPackageTag();
16
    abstract protected function getPackageDefinition($isUrl);
17
    abstract protected function getFallbackPackageDefinition();
18
19 41
    public function __construct($alias, ContainerBuilder $container)
20
    {
21 41
        $this->alias = $alias;
22 41
        $this->container = $container;
23 41
    }
24
25 39
    public function createPackage($name, $config)
26
    {
27 39
        $prefixes = $config['prefix'];
28 39
        $isUrl = Util::containsUrl($prefixes);
29
30 39
        return $this->getPackageDefinition($isUrl)
31 39
            ->addArgument($isUrl ? $prefixes : $prefixes[0])
32 39
            ->addArgument($this->createVersionStrategy($name, $config['manifest']))
33 39
            ->setPublic(false)
34
        ;
35
    }
36
37 39
    public function createFallbackPackage($patterns, $customDefaultPackage)
38
    {
39 39
        return $this->getFallbackPackageDefinition()
40 39
            ->setPublic(false)
41 39
            ->addArgument($patterns)
42 39
            ->addArgument($customDefaultPackage)
43
        ;
44
    }
45
46 39
    public function getPackageId($name)
47
    {
48 39
        return $this->namespaceService("_package.$name");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
49
    }
50
51 39
    protected function createVersionStrategy($packageName, $manifest)
52
    {
53 39
        if ($manifest['enabled']) {
54 8
            return $this->createManifestVersionStrategy($packageName, $manifest);
55
        }
56
57 35
        return $this->createEmptyVersionStrategy($packageName);
58
    }
59
60 35
    protected function createEmptyVersionStrategy($packageName)
61
    {
62 35
        return new Reference($this->namespaceService('version_strategy.empty'));
63
    }
64
65 8
    protected function createManifestVersionStrategy($packageName, $config)
66
    {
67 8
        $loader = new DefinitionDecorator($this->namespaceService('manifest.loader.'.$config['format']));
68
        $loader
69 8
            ->addArgument($config['path'])
70 8
            ->addArgument($config['root_key'])
71
        ;
72
73 8
        $loaderId = $this->namespaceService("_package.$packageName.manifest_loader");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $packageName instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
74 8
        $this->container->setDefinition($loaderId, $loader);
75
76 8
        $cachedLoader = new DefinitionDecorator($this->namespaceService('manifest.loader.cached'));
77 8
        $cachedLoader->addArgument(new Reference($loaderId));
78
79 8
        $cachedLoaderId = $this->namespaceService("_package.$packageName.manifest_loader_cached");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $packageName instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
80 8
        $this->container->setDefinition($cachedLoaderId, $cachedLoader);
81
82 8
        $versionStrategy = new DefinitionDecorator($this->namespaceService('version_strategy.manifest'));
83 8
        $versionStrategy->addArgument(new Reference($cachedLoaderId));
84
85 8
        $versionStrategyId = $this->namespaceService("_package.$packageName.version_strategy");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $packageName instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
86 8
        $this->container->setDefinition($versionStrategyId, $versionStrategy);
87
88 8
        return new Reference($versionStrategyId);
89
    }
90
91 39
    protected function namespaceService($id)
92
    {
93 39
        return $this->alias.'.'.$id;
94
    }
95
}
96