Completed
Pull Request — master (#140)
by Christopher
03:07 queued 55s
created

GroupController::postGroupRoles()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 3
dl 0
loc 30
rs 9.1288
c 0
b 0
f 0
1
<?php
2
3
namespace TechWilk\Rota\Controller;
4
5
use DateTime;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use TechWilk\Rota\EventQuery;
9
use TechWilk\Rota\Group;
10
use TechWilk\Rota\GroupQuery;
11
use TechWilk\Rota\Role;
12
use TechWilk\Rota\RoleQuery;
13
14
class GroupController extends BaseController
15
{
16
    public function getGroup(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
    {
18
        $this->logger->info("Fetch group GET '/group/".$args['id']."'");
19
        $events = EventQuery::create()
20
            ->useEventPersonQuery()
21
                ->useUserRoleQuery()
22
                    ->useRoleQuery()
23
                        ->filterByGroupId($args['id'])
24
                    ->endUse()
25
                ->endUse()
26
            ->endUse()
27
            ->filterByDate(['min' => new DateTime()])
28
            ->filterByRemoved(false)
29
            ->orderByDate('asc')
30
            ->distinct()
31
            ->find();
32
33
        $group = GroupQuery::create()
34
                ->findPk($args['id']);
35
36
        return $this->view->render($response, 'group.twig', ['events' => $events, 'group' => $group]);
37
    }
38
39
    public function getGroupRoles(ServerRequestInterface $request, ResponseInterface $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        $this->logger->info("Fetch group roles GET '/group/".$args['id']."/roles'");
42
43
        $group = GroupQuery::create()
44
                ->findPk($args['id']);
45
46
        return $this->view->render($response, 'group-roles.twig', ['group' => $group]);
47
    }
48
49
    public function postGroup(ServerRequestInterface $request, ResponseInterface $response, $args)
50
    {
51
        $this->logger->info("Create/edit group POST '/group/".$args['id']."'");
52
53
        $group = new Group();
54
55
        if (!empty($args['id'])) {
56
            $group = GroupQuery::create()->findPk($args['id']);
57
        }
58
59
        $data = $request->getParsedBody();
60
61
        $group->setName($data['name']);
62
        $group->setDescription($data['description']);
63
        $group->save();
64
65
        return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('group', ['id' => $group->getId()]));
66
    }
67
68
    public function postGroupRoles(ServerRequestInterface $request, ResponseInterface $response, $args)
69
    {
70
        $this->logger->info("Create/edit group roles POST '/group/".$args['id']."'");
71
72
        $group = GroupQuery::create()->findPk($args['id']);
73
74
        $data = $request->getParsedBody();
75
76
        if (isset($data['name'])) {
77
            $role = new Role();
78
            $role->setName($data['name']);
79
            $role->save();
80
        }
81
82
        if (is_array($data['roles'])) {
83
            foreach ($data['roles'] as $id => $name) {
84
                $role = RoleQuery::create()->findPk($id);
85
86
                if ($name !== $role->getName()) {
87
                    $role->setName($name);
88
                }
89
90
                $role->save();
91
            }
92
93
            return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('group', ['id' => $group->getId()]));
94
        }
95
96
        return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('group-roles', ['id' => $group->getId()]));
97
    }
98
}
99