Completed
Push — master ( 3e75ab...18f3e0 )
by Arnold
02:56
created

TwigAssetic::createExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Jasny\View\Plugin;
4
5
use Jasny\ViewInterface;
6
use Jasny\View\Twig as TwigView;
7
use Jasny\View\PluginInterface;
8
use Assetic\AssetWriter;
9
use Assetic\Extension\Twig\AsseticExtension;
10
use Assetic\Extension\Twig\TwigFormulaLoader;
11
use Assetic\Extension\Twig\TwigResource;
12
use Assetic\Factory\AssetFactory;
13
use Assetic\Factory\LazyAssetManager;
14
15
/**
16
 * Assetic support for Twig
17
 */
18
class TwigAssetic implements PluginInterface
19
{
20
    /**
21
     * @var AssetFactory 
22
     */
23
    protected $factory;
24
25
    /**
26
     * @var AssetWriter
27
     */
28
    protected $writer;
29
    
30
    /**
31
     * Class constructor
32
     * 
33
     * @param AssetFactory $factory
34
     * @param AssetWriter  $writer
35
     */
36 4
    public function __construct(AssetFactory $factory, AssetWriter $writer)
37
    {
38 4
        $this->factory = $factory;
39 4
        $this->writer = $writer;
40 4
    }
41
42
    /**
43
     * Check that the view is a twig view
44
     * 
45
     * @param ViewInterface $view
46
     * @throws \InvalidArgumentException
47
     */
48 4
    protected function assertView(ViewInterface $view)
49
    {
50 4
        if (!$view instanceof TwigView) {
51 2
            throw new \InvalidArgumentException("This plugin only works with a Twig view");
52
        }
53 2
    }
54
    
55
56
    /**
57
     * Create an assetic extension for Twig.
58
     * @codeCoverageIgnore
59
     * 
60
     * @return AsseticExtension
61
     */
62
    protected function createExtension()
63
    {
64
        return new AsseticExtension($this->factory);
65
    }
66
67
    /**
68
     * Create an assetic formula loader.
69
     * @codeCoverageIgnore
70
     * 
71
     * @param \Twig_Environment $twig
72
     * @return TwigFormulaLoader
73
     */
74
    protected function createFormulaLoader($twig)
75
    {
76
        return new TwigFormulaLoader($twig);
77
    }
78
    
79
    /**
80
     * Create an assetic asset manager.
81
     * @codeCoverageIgnore
82
     * 
83
     * @param TwigFormulaLoader $loader
84
     * @return LazyAssetManager
85
     */
86
    protected function createAssetManager(TwigFormulaLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $loader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        return new LazyAssetManager($this->factory);
89
    }
90
    
91
    /**
92
     * Create an assetic twig resource.
93
     * @codeCoverageIgnore
94
     * 
95
     * @param \Twig_Environment $twig
96
     * @param string            $template
97
     * @return TwigResource
98
     */
99
    protected function createResource($twig, $template)
100
    {
101
        return new TwigResource($twig->getLoader(), $template);
102
    }
103
    
104
105
    /**
106
     * Called when the plugin is added to the view.
107
     * 
108
     * @param ViewInterface $view
109
     */
110 3
    public function onAdd(ViewInterface $view)
111
    {
112 3
        $this->assertView($view);
113
114 1
        $view->getTwig()->addExtension($this->createExtension());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Jasny\ViewInterface as the method getTwig() does only exist in the following implementations of said interface: Jasny\View\Twig.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
115 1
    }
116
    
117
    /**
118
     * Called when view renders a template.
119
     * 
120
     * @param ViewInterface $view
121
     * @param string        $template   Template filename
122
     * @param array         $context
123
     */
124 1
    public function onRender(ViewInterface $view, $template, array $context)
125
    {
126 1
        $this->assertView($view);
127
        
128 1
        $twig = $view->getTwig();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Jasny\ViewInterface as the method getTwig() does only exist in the following implementations of said interface: Jasny\View\Twig.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
129
        
130 1
        $loader = $this->createFormulaLoader($twig);
131
        
132 1
        $assetManager = $this->createAssetManager($loader);
133 1
        $assetManager->setLoader('twig', $loader);        
134
        
135 1
        $resource = $this->createResource($twig, $template);
136 1
        $assetManager->addResource($resource, 'twig');
137
        
138 1
        $this->writer->writeManagerAssets($assetManager);
139 1
    }
140
}
141