AbstractDrawStrategy   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 58
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A attach() 0 4 1
A shouldRespond() 0 13 4
A collectModelVariables() 0 9 2
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw/ for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\View\Strategy;
12
13
use WebinoDraw\Service\DrawService;
14
use Zend\EventManager\EventManagerInterface;
15
use Zend\Http\PhpEnvironment\Response as PhpResponse;
16
use Zend\View\Model\ViewModel;
17
use Zend\View\Renderer\PhpRenderer;
18
use Zend\View\Strategy\PhpRendererStrategy;
19
use Zend\View\ViewEvent;
20
21
/**
22
 * Draw XHTML with this view strategy
23
 */
24
abstract class AbstractDrawStrategy extends PhpRendererStrategy
25
{
26
    /**
27
     * @var DrawService $draw
28
     */
29
    protected $draw;
30
31
    /**
32
     * @param DrawService $draw
33
     */
34
    public function __construct(DrawService $draw)
35
    {
36
        $this->draw = $draw;
37
    }
38
39
    /**
40
     * @param EventManagerInterface $events
41
     * @param int $priority
42
     */
43
    public function attach(EventManagerInterface $events, $priority = 1)
44
    {
45
        parent::attach($events, $priority - 100); // as last
46
    }
47
48
    /**
49
     * @param ViewEvent $event
50
     * @return bool
51
     */
52
    public function shouldRespond(ViewEvent $event)
53
    {
54
        /* @var $response PhpResponse */
55
        $response = $event->getResponse();
56
        if ($event->getRenderer() instanceof PhpRenderer
57
            && $response instanceof PhpResponse
58
            && trim($response->getBody())
59
        ) {
60
            return true;
61
        }
62
63
        return false;
64
    }
65
66
    /**
67
     * Collect all variables from the view model
68
     *
69
     * @param ViewModel $model
70
     * @return array
71
     */
72
    public function collectModelVariables(ViewModel $model)
73
    {
74
        $vars = (array) $model->getVariables();
75
        foreach ($model->getChildren() as $child) {
76
            $vars = array_replace($vars, $this->collectModelVariables($child));
77
        }
78
79
        return $vars;
80
    }
81
}
82