Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
TagConnectionController
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
2 / 2
15
100.00% covered (success)
100.00%
1 / 1
 add
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
6
 remove
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3class TagConnectionController extends AppController
4{
5    public function add($tsumegoID, $tagName)
6    {
7        if (!Auth::isLoggedIn())
8        {
9            $this->response->statusCode(403);
10            $this->response->body('Not logged in.');
11            return $this->response;
12        }
13        $tag = ClassRegistry::init('Tag')->findByName($tagName);
14        if (!$tag)
15        {
16            $this->response->statusCode(403);
17            $this->response->body('Tag "' . $tagName . '" doesn\'t exist.');
18            return $this->response;
19        }
20
21        $tsumego = ClassRegistry::init('Tsumego')->findById($tsumegoID);
22        if (!$tsumego)
23        {
24            $this->response->statusCode(403);
25            $this->response->body('Tsumego with id="' . $tsumegoID . '" wasn\'t found.');
26            return $this->response;
27        }
28
29        $tagConnection = ClassRegistry::init('TagConnection')->find('first', [
30            'conditions' => [
31                'tsumego_id' => $tsumegoID,
32                'tag_id' => $tag['Tag']['id']]]);
33        if ($tagConnection)
34        {
35            $this->response->statusCode(403);
36            $this->response->body('The tsumego already has tag ' . $tag['Tag']['name'] . '.');
37            return $this->response;
38        }
39
40        $tagConnection = [];
41        $tagConnection['tag_id'] = $tag['Tag']['id'];
42        $tagConnection['tsumego_id'] = $tsumegoID;
43        $tagConnection['user_id'] = Auth::getUserID();
44        $tagConnection['approved'] = Auth::isAdmin() ? 1 : 0;
45        ClassRegistry::init('TagConnection')->create();
46        ClassRegistry::init('TagConnection')->save($tagConnection);
47        $this->response->statusCode(200);
48        return $this->response;
49    }
50
51    public function remove($tsumegoID, $tagName)
52    {
53        if (!Auth::isLoggedIn())
54        {
55            $this->response->statusCode(403);
56            $this->response->body('Not logged in.');
57            return $this->response;
58        }
59
60        $tag = ClassRegistry::init('Tag')->findByName($tagName);
61        if (!$tag)
62        {
63            $this->response->statusCode(403);
64            $this->response->body('Tag "' . $tagName . '" doesn\'t exist.');
65            return $this->response;
66        }
67
68        $tsumego = ClassRegistry::init('Tsumego')->findById($tsumegoID);
69        if (!$tsumego)
70        {
71            $this->response->statusCode(403);
72            $this->response->body('Tsumego with id="' . $tsumegoID . '" wasn\'t found.');
73            return $this->response;
74        }
75
76        $tagConnection = ClassRegistry::init('TagConnection')->find('first', ['conditions' => [
77            'tag_id' => $tag['Tag']['id'],
78            'tsumego_id' => $tsumegoID]]);
79        if (!$tagConnection)
80        {
81            $this->response->statusCode(403);
82            $this->response->body('Tag to remove isn\'t assigned to this tsumego.');
83            return $this->response;
84        }
85
86        if ($tagConnection['TagConnection']['approved'] == 1 && !Auth::isAdmin())
87        {
88            $this->response->statusCode(403);
89            $this->response->body('Only admins can remove approved tags.');
90            return $this->response;
91        }
92
93        if ($tagConnection['TagConnection']['user_id'] != Auth::getUserID() && !Auth::isAdmin())
94        {
95            $this->response->statusCode(403);
96            $this->response->body('You can\'t remove tag proposed by someone else.');
97            return $this->response;
98        }
99
100        ClassRegistry::init('TagConnection')->delete($tagConnection['TagConnection']['id']);
101        $this->response->statusCode(200);
102        return $this->response;
103    }
104}