Completed
Push — master ( 208ea1...3e75ab )
by Arnold
06:06
created

TwigAssetic::assertView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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\TwigFormulaLoader;
10
use Assetic\Extension\Twig\TwigResource;
11
use Assetic\Factory\AssetFactory;
12
use Assetic\Factory\LazyAssetManager;
13
14
/**
15
 * Assetic support for Twig
16
 */
17
class TwigAssetic implements PluginInterface
18
{
19
    /**
20
     * @var AssetFactory 
21
     */
22
    protected $factory;
23
24
    /**
25
     * @var AssetWriter
26
     */
27
    protected $writer;
28
    
29
    /**
30
     * Class constructor
31
     * 
32
     * @param AssetFactory $factory
33
     * @param AssetWriter  $writer
34
     */
35 4
    public function __construct(AssetFactory $factory, AssetWriter $writer)
36
    {
37 4
        $this->factory = $factory;
38 4
        $this->writer = $writer;
39 4
    }
40
41
    /**
42
     * Check that the view is a twig view
43
     * 
44
     * @param ViewInterface $view
45
     * @throws \InvalidArgumentException
46
     */
47 4
    protected function assertView(ViewInterface $view)
48
    {
49 4
        if (!$view instanceof TwigView) {
50 2
            throw new \InvalidArgumentException("This plugin only works with a Twig view");
51
        }
52 2
    }
53
    
54
    /**
55
     * Called when the plugin is added to the view.
56
     * 
57
     * @param ViewInterface $view
58
     */
59 3
    public function onAdd(ViewInterface $view)
60
    {
61 3
        $this->assertView($view);
62 1
    }
63
    
64
    
65
    /**
66
     * Create an assetic formula loader.
67
     * @codeCoverageIgnore
68
     * 
69
     * @param \Twig_Environment $twig
70
     * @return TwigFormulaLoader
71
     */
72
    protected function createFormulaLoader($twig)
73
    {
74
        return new TwigFormulaLoader($twig);
75
    }
76
    
77
    /**
78
     * Create an assetic asset manager.
79
     * @codeCoverageIgnore
80
     * 
81
     * @param TwigFormulaLoader $loader
82
     * @return LazyAssetManager
83
     */
84
    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...
85
    {
86
        return new LazyAssetManager($this->factory);
87
    }
88
    
89
    /**
90
     * Create an assetic twig resource.
91
     * @codeCoverageIgnore
92
     * 
93
     * @param \Twig_Environment $twig
94
     * @param string            $template
95
     * @return TwigResource
96
     */
97
    protected function createResource($twig, $template)
98
    {
99
        return new TwigResource($twig->getLoader(), $template);
100
    }
101
    
102
103
    /**
104
     * Called when view renders a template.
105
     * 
106
     * @param ViewInterface $view
107
     * @param string        $template   Template filename
108
     * @param array         $context
109
     */
110 1
    public function onRender(ViewInterface $view, $template, array $context)
111
    {
112 1
        $this->assertView($view);
113
        
114 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...
115
        
116 1
        $loader = $this->createFormulaLoader($twig);
117
        
118 1
        $assetManager = $this->createAssetManager($loader);
119 1
        $assetManager->setLoader('twig', $loader);        
120
        
121 1
        $resource = $this->createResource($twig, $template);
122 1
        $assetManager->addResource($resource, 'twig');
123
        
124 1
        $this->writer->writeManagerAssets($assetManager);
125 1
    }
126
}
127