Completed
Push — develop ( dfd31d...f6e11d )
by
unknown
17:02
created

InsertFile::getEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * YAWIK
5
 *
6
 * @filesource
7
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
8
 * @license   MIT
9
 */
10
11
namespace Core\View\Helper;
12
13
use Zend\View\ViewEvent;
14
use Core\View\Helper\InsertFile\FileEvent;
15
use Zend\ServiceManager\ServiceLocatorInterface;
16
use Zend\View\HelperPluginManager;
17
18
class InsertFile extends AbstractEventsHelper
19
{
20
    
21
    /**
22
     * @var ServiceLocatorInterface
23
     */
24
    protected $serviceManager;
25
    
26
    protected $files = array();
27
    
28
    protected $ListenersUnaware = true;
0 ignored issues
show
Coding Style introduced by
$ListenersUnaware does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
29
    
30
    protected $event;
31
32
    /**
33
     * @param ServiceLocatorInterface $serviceManager
34
     * @param string $identifiers
0 ignored issues
show
Documentation introduced by
Should the type for parameter $identifiers not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
35
     */
36
    public function __construct(ServiceLocatorInterface $serviceManager, $identifiers = null)
37
    {
38
        parent::__construct($identifiers);
39
        $this->serviceManager = $serviceManager;
40
    }
41
    
42
    /**
43
     * render a File-Object
44
     *
45
     * @param string
46
     * @return string
47
     */
48
    public function __invoke($fileName, $parameter = array())
49
    {
50
        $this->listenToRenderer();
51
        $event = $this->getEvent();
52
        $event->addFilename($fileName);
53
        $event->setRenderParameter($parameter);
54
        $return = 'file not found';
55
        
56
        // ensure, that we have a file-object
57
        $result = $this->trigger(FileEvent::GETFILE, $event);
58
        if (!empty($result)) {
59
            // ask someone else for rendering
60
            $result = $this->trigger(FileEvent::RENDERFILE, $event);
61
            $return = $result->last();
62
        }
63
        return $return;
64
    }
65
    
66
    protected function getEvent()
67
    {
68
        if (!isset($this->event)) {
69
            $this->event = new FileEvent();
70
        }
71
        return $this->event;
72
    }
73
       
74
    /**
75
     * hook into the rendering-process to provide a summary of all included files
76
     */
77
    public function listenToRenderer()
78
    {
79
        if ($this->ListenersUnaware) {
80
            // set a listener at the end of the Rendering-Process
81
            // to announce what files have been inserted
82
            $this->ListenersUnaware = false;
83
            $view = $this->serviceManager->get('View');
84
            $viewEvents = $view->getEventManager();
85
            // rendering ist over
86
            // get the attached Files very early
87
            // before any other postprocessing has started
88
            $viewEvents->attach(ViewEvent::EVENT_RESPONSE, array($this, 'anounceAttachedFiles'), 1000);
89
        }
90
    }
91
    
92
    public function anounceAttachedFiles(ViewEvent $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e 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...
93
    {
94
        $event = $this->getEvent();
95
        $this->trigger(FileEvent::INSERTFILE, $event);
96
    }
97
    
98
    /**
99
     * @param HelperPluginManager $helperPluginManager
100
     * @return InsertFile
101
     */
102
    public static function factory(HelperPluginManager $helperPluginManager)
103
    {
104
        return new static($helperPluginManager->getServiceLocator());
105
    }
106
}
107