Completed
Push — develop ( 41c7f2...c75883 )
by
unknown
08:12
created

AdminControllerEvent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Controller;
12
13
use ArrayAccess;
14
use Zend\EventManager\Event;
15
use Zend\Stdlib\PriorityList;
16
use Zend\View\Model\ViewModel;
17
18
/**
19
 * ${CARET}
20
 * 
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @todo write test 
23
 */
24
class AdminControllerEvent extends Event
25
{
26
    const EVENT_DASHBOARD = 'DASHBOARD_FETCH_MODELS';
27
28
    protected $models;
29
    protected $data;
30
31
    public function __construct($name = null, $target = null, $params = null)
32
    {
33
        parent::__construct($name, $target, $params);
34
        $this->models = new PriorityList();
35
    }
36
37
    public function addViewModel($name, $model, $priority=0)
38
    {
39
        $this->models->insert($name, $model, $priority);
40
41
        return $this;
42
    }
43
44
    public function addViewTemplate($name, $template, $vars = [], $priority = 0)
45
    {
46
        if (is_int($vars)) {
47
            $priority = $vars;
48
            $vars = [];
49
        }
50
51
        $model = new ViewModel($vars);
52
        $model->setTemplate($template);
53
54
        return $this->addViewModel($name, $model, $priority);
55
    }
56
57
    public function getViewModels()
58
    {
59
        return $this->models->getIterator();
60
    }
61
62
    public function addViewVariables($name, $data = [], $priority = 0)
63
    {
64
        if (is_array($name)) {
65
            if (!isset($name['name'])) {
66
                throw new \DomainException('Key "name" must be specified, if array is passed as first parameter.');
67
            }
68
            if (is_int($data)) {
69
                $priority = $data;
70
            }
71
            $data = $name;
72
            $name = $data['name'];
73
74
        }
75
76
        if (!isset($data['name'])) {
77
            $data['name'] = $name;
78
        }
79
80
        return $this->addViewTemplate($name, "core/admin/dashboard-widget", $data, $priority);
81
    }
82
83
84
}