Completed
Pull Request — master (#273)
by
unknown
01:12
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
    public function get()
24
    {
25
        $watched = $this->getWatches()->getWatchedFunctions();
26
27
        $this->_template = 'watch/list.twig';
28
        $this->set(array('watched' => $watched));
29
    }
30
31
    public function post()
32
    {
33
        $app = $this->app;
34
        $watches = $this->watches;
35
36
        $saved = false;
37
        $request = $app->request();
38
        foreach ((array)$request->post('watch') as $data) {
39
            if (empty($data['id'])) {
40
                $watches->addWatchedFunction($data['name']);
41
            } elseif (!empty($data['removed']) && $data['removed'] === '1') {
42
                $watches->removeWatchedFunction($data['id']);
43
            } else {
44
                $watches->updateWatchedFunction($data['id'], $data['name']);
45
            }
46
            $saved = true;
47
        }
48
        if ($saved) {
49
            $app->flash('success', 'Watch functions updated.');
50
        }
51
        $app->redirect($app->urlFor('watch.list'));
52
    }
53
54
    /**
55
     * @return Xhgui_WatchedFunctionsStorageInterface
56
     */
57
    public function getWatches()
58
    {
59
        return $this->watches;
60
    }
61
62
    /**
63
     * @param Xhgui_WatchedFunctionsStorageInterface $watches
64
     */
65
    public function setWatches($watches)
66
    {
67
        $this->watches = $watches;
68
    }
69
}
70