Completed
Push — gocardless-upgrade ( 88f819...8f5bab )
by Arthur
11:53 queued 07:44
created

ActivityController::store()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 3
eloc 21
nc 2
nop 1
1
<?php
2
3
namespace BB\Http\Controllers\ACS;
4
5
use BB\Entities\ACSNode;
6
use BB\Events\MemberActivity;
7
use BB\Repo\ACSNodeRepository;
8
use BB\Repo\EquipmentLogRepository;
9
use BB\Services\KeyFobAccess;
10
use Illuminate\Http\Request;
11
use BB\Http\Requests;
12
use BB\Http\Controllers\Controller;
13
use Swagger\Annotations as SWG;
14
15
class ActivityController extends Controller
16
{
17
    /**
18
     * @var ACSNodeRepository
19
     */
20
    private $ACSNodeRepository;
21
    /**
22
     * @var EquipmentLogRepository
23
     */
24
    private $equipmentLogRepository;
25
    /**
26
     * @var KeyFobAccess
27
     */
28
    private $fobAccess;
29
30
    /**
31
     * NodeController constructor.
32
     *
33
     * @param ACSNodeRepository $ACSNodeRepository
34
     */
35
    public function __construct(ACSNodeRepository $ACSNodeRepository, EquipmentLogRepository $equipmentLogRepository, KeyFobAccess $fobAccess)
36
    {
37
        $this->ACSNodeRepository = $ACSNodeRepository;
38
        $this->equipmentLogRepository = $equipmentLogRepository;
39
        $this->fobAccess = $fobAccess;
40
    }
41
42
    /**
43
     * Record the start of a new session
44
     *
45
     * @return \Illuminate\Http\Response
46
     *
47
     * @SWG\Post(
48
     *     path="/acs/activity",
49
     *     tags={"activity"},
50
     *     description="Record the start of a period of activity, e.g. someone signing into the laser cutter. If an entry device is specified no equipment access record is started but an activity log is created",
51
     *     @SWG\Parameter(name="activity", in="body", required=true, @SWG\Schema(ref="#/definitions/Activity")),
52
     *     @SWG\Response(response="201", description="Activity started, the body will contain the new activityId"),
53
     *     @SWG\Response(response="404", description="Key fob not found"),
54
     *     security={{"api_key": {}}}
55
     * )
56
     */
57
    public function store(Request $request)
58
    {
59
        $activityRequest = new Requests\ACS\Activity($request);
60
61
        $keyFob = $this->fobAccess->extendedKeyFobLookup($activityRequest->getTagId());
62
63
        // lookup the acs node
64
        $node = ACSNode::where('device_id', $activityRequest->getDevice())->firstOrFail();
65
        if ($node->entry_device) {
66
            $this->fobAccess->verifyForEntry($activityRequest->getTagId(), $activityRequest->getDevice(), $activityRequest->getOccurredAt());
67
            $this->fobAccess->logSuccess();
68
        } else {
69
            $activityId = $this->equipmentLogRepository->recordStartCloseExisting($keyFob->user->id, $keyFob->id, $activityRequest->getDevice());
70
            event(new MemberActivity($keyFob, $activityRequest->getDevice()));
71
        }
72
73
74
75
        return response()->json([
76
            'activityId' => isset($activityId)? $activityId: null,
77
            'user'       => [
78
                'id'              => $keyFob->user->id,
79
                'name'            => $keyFob->user->name,
80
                'status'          => $keyFob->user->status,
81
                'active'          => $keyFob->user->active,
82
                'key_holder'      => $keyFob->user->key_holder,
83
                'cash_balance'    => $keyFob->user->cash_balance,
0 ignored issues
show
Documentation introduced by
The property cash_balance does not exist on object<BB\Entities\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
84
                'profile_private' => $keyFob->user->profile_private,
85
            ]
86
        ], 201);
87
    }
88
89
    /**
90
     * Update an ongoing activity
91
     *
92
     * @return \Illuminate\Http\Response
93
     *
94
     * @SWG\Put(
95
     *     path="/acs/activity/{activityId}",
96
     *     tags={"activity"},
97
     *     description="Record a heartbeat message for a period of activity, used to ensure activity periods are correctly recorded",
98
     *     @SWG\Parameter(name="activityId", in="path", type="string", required=true),
99
     *     @SWG\Response(response="200", description="Activity Heartbeat recorded"),
100
     *     security={{"api_key": {}}}
101
     * )
102
     */
103 View Code Duplication
    public function update(Request $request, $activityId)
104
    {
105
        $keyFob = $this->fobAccess->extendedKeyFobLookup($request->get('tagId'));
106
107
        $this->equipmentLogRepository->recordActivity($activityId);
108
109
        return response()->json([], 200);
110
    }
111
112
    /**
113
     * Show the form for creating a new resource.
114
     *
115
     * @return \Illuminate\Http\Response
116
     *
117
     * @SWG\Delete(
118
     *     path="/acs/activity/{activityId}",
119
     *     tags={"activity"},
120
     *     description="End a period of an activity",
121
     *     @SWG\Parameter(name="activityId", in="path", type="string", required=true),
122
     *     @SWG\Response(response="204", description="Activity ended/deleted"),
123
     *     @SWG\Response(response="400", description="Session invalid"),
124
     *     security={{"api_key": {}}}
125
     * )
126
     */
127 View Code Duplication
    public function destroy(Request $request, $activityId)
128
    {
129
        $keyFob = $this->fobAccess->extendedKeyFobLookup($request->get('tagId'));
130
131
        $this->equipmentLogRepository->endSession($activityId);
132
133
        return response()->json([], 204);
134
    }
135
136
}
137