RedirectTracker::getModelAttributes()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 8.2222
cc 7
eloc 10
nc 4
nop 1
crap 7
1
<?php
2
3
namespace BoxedCode\Tracking\Trackers;
4
5
use BoxedCode\Tracking\Contracts\Tracker as TrackerContract;
6
use BoxedCode\Tracking\TrackableResourceModel;
7
use Illuminate\Http\Request;
8
use InvalidArgumentException;
9
10
class RedirectTracker extends Tracker implements TrackerContract
11
{
12
    /**
13
     * The trackers type handle.
14
     *
15
     * @var string
16
     */
17
    protected $handle = 'url';
18
19
    /**
20
     * The trackers route name.
21
     *
22
     * @var string
23
     */
24
    protected $route_name = 'tracking.redirect';
25
26
    /**
27
     * The trackers route parameter.
28
     *
29
     * @var string
30
     */
31
    protected $route_parameter = 'r';
32
33
    /**
34
     * Handle the tracking request.
35
     *
36
     * @param \Illuminate\Http\Request $request
37
     * @param \BoxedCode\Tracking\TrackableResourceModel $model
38
     * @return mixed
39
     */
40 3
    public function handle(Request $request, TrackableResourceModel $model)
41
    {
42 3
        $default_status = $this->config->get('tracking.redirect', 302);
43
44 3
        $status_code = $model->meta['status_code'] ?: $default_status;
45
46 3
        return redirect()->away($model->resource, $status_code);
47
    }
48
49
    /**
50
     * Generate a model attribute array from an argument array.
51
     *
52
     * @param array $args
53
     * @return array
54
     */
55 3
    public function getModelAttributes(array $args)
56
    {
57 3
        if (! isset($args[0]) || ! filter_var($args[0], FILTER_VALIDATE_URL)) {
58 1
            $url = isset($args[0]) ? (string) $args[0] : 'none';
59
60 1
            throw new InvalidArgumentException("Invalid url provided. [$url]");
61
        }
62
63 2
        if (isset($args[1]) && ! is_int($args[1])) {
64 1
            throw new InvalidArgumentException("Invalid status code. [$args[1]]");
65
        }
66
67 1
        return parent::getModelAttributes([
68 1
            'resource' => $args[0],
69
            'meta' => [
70 1
                'status_code' => isset($args[1]) ? $args[1] : null,
71 1
            ],
72 1
        ]);
73
    }
74
}
75