Completed
Pull Request — master (#268)
by
unknown
01:13
created

Xhgui_Controller_Watch::post()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
cc 6
nc 8
nop 0
1
<?php
2
3
use Slim\Slim;
4
5
class Xhgui_Controller_Watch extends Xhgui_Controller
6
{
7
    /**
8
     * @var \Xhgui_WatchedFunctionsStorageInterface
9
     */
10
    protected $watches;
11
12
    /**
13
     * Xhgui_Controller_Watch constructor.
14
     * @param Slim $app
15
     * @param Xhgui_WatchedFunctionsStorageInterface $watches
16
     */
17
    public function __construct(Slim $app, \Xhgui_WatchedFunctionsStorageInterface $watches)
18
    {
19
        parent::__construct($app);
20
        $this->setWatches($watches);
21
    }
22
23
    /**
24
     *
25
     */
26
    public function get()
27
    {
28
        $watched = $this->getWatches()->getWatchedFunctions();
29
30
        $this->_template = 'watch/list.twig';
31
        $this->set(array('watched' => $watched));
32
    }
33
34
    /**
35
     *
36
     */
37
    public function post()
38
    {
39
        $app     = $this->app;
40
        $watches = $this->watches;
41
42
        $saved = false;
43
        $request = $app->request();
44
        foreach ((array)$request->post('watch') as $data) {
45
            if (empty($data['id'])) {
46
                $watches->addWatchedFunction($data['name']);
47
            } elseif (!empty($data['removed']) && $data['removed'] === '1') {
48
                $watches->removeWatchedFunction($data['id']);
49
            } else {
50
                $watches->updateWatchedFunction($data['id'], $data['name']);
51
            }
52
            $saved = true;
53
        }
54
        if ($saved) {
55
            $app->flash('success', 'Watch functions updated.');
56
        }
57
        $app->redirect($app->urlFor('watch.list'));
58
    }
59
60
    /**
61
     * @return Xhgui_WatchedFunctionsStorageInterface
62
     */
63
    public function getWatches()
64
    {
65
        return $this->watches;
66
    }
67
68
    /**
69
     * @param Xhgui_WatchedFunctionsStorageInterface $watches
70
     */
71
    public function setWatches($watches)
72
    {
73
        $this->watches = $watches;
74
    }
75
}
76