Completed
Push — FVSv2 ( 1ec6b9...63e0fa )
by Patrick
01:36
created

EventAPI   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 109
Duplicated Lines 15.6 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 17
loc 109
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setup() 0 6 1
A canUpdate() 0 8 2
A canDelete() 0 8 2
B processEntry() 0 21 6
A getShiftsForEvent() 0 34 5
A createShiftForEvent() 17 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
class EventAPI extends VolunteerAPI
3
{
4
    use Processor;
5
6
    public function __construct()
7
    {
8
        parent::__construct('events');
9
    }
10
11
    public function setup($app)
12
    {
13
        parent::setup($app);
14
        $app->get('/{event}/shifts[/]', array($this, 'getShiftsForEvent'));
15
        $app->post('/{event}/shifts[/]', array($this, 'createShiftForEvent'));
16
    }
17
18
    protected function canUpdate($request, $entity)
19
    {
20
 	if($this->isVolunteerAdmin($request))
21
        {
22
            return true;
23
        }       
24
        return false;
25
    }
26
27
    protected function canDelete($request, $entity)
28
    {
29
        if($this->isVolunteerAdmin($request))
30
        {
31
            return true;
32
        }
33
        return false;
34
    }
35
36
    public function processEntry($entry, $request)
37
    {
38
        $entry['available'] = true;
39
        $endTime = new DateTime($entry['endTime']);
40
        $now = new DateTime();
41
        if($endTime < $now)
42
        {
43
            $entry['available'] = false;
44
            $entry['why'] = 'Event is in the past';
45
        }
46
        if($entry['private'] && !in_array($this->user->mail, $entry['volList']))
1 ignored issue
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
        {
48
            $entry['available'] = false;
49
            $entry['why'] = 'Event is private and you are not invited';
50
        }
51
        if(!$entry['available'] && !$this->isVolunteerAdmin($request))
52
        {
53
            return null;
54
        }
55
        return $entry;
56
    }
57
58
    public function getShiftsForEvent($request, $response, $args)
59
    {
60
        $this->validateLoggedIn($request);
61
        $eventId = $args['event'];
62
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
63
        $filter = new \Data\Filter("eventID eq '$eventId'");
64
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
65
        if($odata->filter !== false)
66
        {
67
            $clause = $odata->filter->getClause('eventID');
68
            if($clause !== null)
69
            {
70
                return $response->withStatus(409);
71
            }
72
            else
73
            {
74
                $filter->appendChild('and');
75
                $filter->appendChild($odata->filter);
76
            }
77
        }
78
        $shifts = $dataTable->read($filter, $odata->select, $odata->top,
79
                                  $odata->skip, $odata->orderby);
80
        if($shifts === false)
81
        {
82
            $shifts = array();
83
        }
84
        $count = count($shifts);
85
        for($i = 0; $i < $count; $i++)
86
        {
87
            $shifts[$i] = $this->processShift($shifts[$i], $request);
88
        }
89
        $shifts = array_values(array_filter($shifts));
90
        return $response->withJson($shifts);
91
    }
92
93 View Code Duplication
    public function createShiftForEvent($request, $response, $args)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $eventId = $args['event'];
96
        if($this->canUpdate($request, null) === false)
97
        {
98
            return $response->withStatus(401);
99
        }
100
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
101
        $obj = $request->getParsedBody();
102
        if($obj == NULL)
103
        {
104
            $obj = json_decode($request->getBody()->getContents(), true);
105
        }
106
        $obj['eventID'] = $eventId;
107
        $ret = $dataTable->create($obj);
108
        return $response->withJson($ret);
109
    }
110
}
111