Completed
Push — master ( d96a7a...d56a4b )
by Mahmoud
04:03
created

TrackOpenAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace App\Containers\Tracker\Actions;
4
5
use App\Containers\Tracker\Models\TimeTracker;
6
use App\Containers\Tracker\Services\FindTimeTrackerService;
7
use App\Containers\Tracker\Settings\Repositories\TimeTrackerRepository;
8
use App\Containers\User\Services\FindUserService;
9
use App\Port\Action\Abstracts\Action;
10
use Carbon\Carbon;
11
12
/**
13
 * Class TrackOpenAction.
14
 *
15
 * @author Mahmoud Zalt <[email protected]>
16
 */
17
class TrackOpenAction extends Action
18
{
19
20
    /**
21
     * @var  \App\Containers\User\Services\FindUserService
22
     */
23
    private $findUserService;
24
25
    /**
26
     * @var  \App\Containers\Tracker\Settings\Repositories\TimeTrackerRepository
27
     */
28
    private $timeTrackerRepository;
29
30
    /**
31
     * @var  \App\Containers\Tracker\Services\FindTimeTrackerService
32
     */
33
    private $findTimeTrackerService;
34
35
    /**
36
     * TrackOpenAction constructor.
37
     *
38
     * @param \App\Containers\User\Services\FindUserService                       $findUserService
39
     * @param \App\Containers\Tracker\Settings\Repositories\TimeTrackerRepository $timeTrackerRepository
40
     */
41
    public function __construct(
42
        FindUserService $findUserService,
43
        TimeTrackerRepository $timeTrackerRepository,
44
        FindTimeTrackerService $findTimeTrackerService
45
    ) {
46
        $this->findUserService = $findUserService;
47
        $this->timeTrackerRepository = $timeTrackerRepository;
48
        $this->findTimeTrackerService = $findTimeTrackerService;
49
    }
50
51
    /**
52
     * @param $email
53
     * @param $password
54
     *
55
     * @return mixed
56
     */
57
    public function run($visitorId)
58
    {
59
        $user = $this->findUserService->byVisitorId($visitorId);
60
61
        // check if any previous session was not closed
62
        $timeTracker = $this->findTimeTrackerService->byUserIdAndStatusNull($user->id);
63
        if ($timeTracker && $timeTracker->status == TimeTracker::PENDING) {
64
            $this->timeTrackerRepository->update(['status' => TimeTracker::FAILED], $timeTracker->id);
65
        }
66
67
        // create the new record with pending status
68
        $timeTracker = new TimeTracker();
69
        $timeTracker->open_at = Carbon::now();
0 ignored issues
show
Documentation introduced by
The property open_at does not exist on object<App\Containers\Tracker\Models\TimeTracker>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
70
        $timeTracker->status = TimeTracker::PENDING;
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<App\Containers\Tracker\Models\TimeTracker>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
71
        $timeTracker->user()->associate($user);
72
        $timeTracker = $this->timeTrackerRepository->create($timeTracker->toArray());
73
74
        return $timeTracker;
75
    }
76
}
77