Completed
Push — master ( fda8a7...2caac5 )
by Renato
06:23
created

DefaultHandler::log()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 2
b 0
f 0
nc 12
nop 5
dl 0
loc 26
ccs 19
cts 19
cp 1
crap 5
rs 8.439
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_name = '';
41 1
        if ($user) {
42 1
            $user_id = $user->getAuthIdentifier();
43 1
            $field_username = config('nwlaravel.activity.field_username');
44 1
            $user_name = (isset($user->{$field_username})) ? $user->{$field_username} : '';
45 1
        }
46
        
47 1
        $content_type = is_object($content) ? get_class($content) : null;
48 1
        $content_id = is_object($content) ? $content->id : null;
49
50
        $data = [
51 1
            'action' => $action,
52 1
            'user_id' => $user_id,
53 1
            'user_name' => $user_name,
54 1
            'description' => $description,
55
            // 'details' => null,
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56 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...
57 1
            'content_type' => $content_type,
58 1
            'content_id' => $content_id,
59 1
        ];
60
61 1
        return (bool) $this->model->create($data);
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
}