DefaultHandler::cleanLog()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace NwLaravel\ActivityLog\Handlers;
3
4
use Illuminate\Http\Request;
5
use Illuminate\Contracts\Auth\Authenticatable;
6
use NwLaravel\ActivityLog\ActivityLog;
7
use Carbon\Carbon;
8
9
class DefaultHandler implements HandlerInterface
10
{
11
    /**
12
     * @var ActivityLog
13
     */
14
    protected $model;
15
16
    /**
17
     * Construct
18
     *
19
     * @param ActivityLog $model
20
     */
21 2
    public function __construct(ActivityLog $model)
22
    {
23 2
        $this->model = $model;
24 2
    }
25
26
    /**
27
     * Log activity
28
     *
29
     * @param string          $action
30
     * @param string          $description
31
     * @param \Eloquent       $content
32
     * @param Authenticatable $user
33
     * @param Request         $request
34
     *
35
     * @return bool
36
     */
37 1
    public function log($action, $description, $content = null, Authenticatable $user = null, Request $request = null)
38
    {
39 1
        $user_id = null;
40 1
        $user_type = null;
41 1
        $user_name = null;
42 1
        if ($user) {
43 1
            $user_id = $user->getAuthIdentifier();
44 1
            $user_type = get_class($user);
45 1
            $user_name = $user->getAuthIdentifierName();
46 1
        }
47
        
48 1
        $content_type = is_object($content) ? get_class($content) : null;
49 1
        $content_id = is_object($content) ? $content->id : null;
50 1
        $ip_address = $request->ip();
0 ignored issues
show
Bug introduced by
It seems like $request is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
51
52 1
        return (bool) $this->model->create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on NwLaravel\ActivityLog\ActivityLog. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
53 1
            'action' => $action,
54 1
            'user_id' => $user_id,
55 1
            'user_type' => $user_type,
56 1
            'user_name' => $user_name,
57 1
            'description' => $description,
58 1
            'ip_address' => $ip_address,
59 1
            'content_type' => $content_type,
60 1
            'content_id' => $content_id,
61 1
        ]);
62
    }
63
64
    /**
65
     * Clean old log records.
66
     *
67
     * @param int $maxAgeInMonths
68
     *
69
     * @return integer
70
     */
71 1
    public function cleanLog($maxAgeInMonths)
72
    {
73 1
        $maxAgeInMonths = 4;
74 1
        $date = Carbon::now()->subMonths($maxAgeInMonths);
75
76 1
        return $this->model->where('created_at', '<=', $date->format('Y-m-d'))->delete();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<NwLaravel\ActivityLog\ActivityLog>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77
    }
78
}
79