RedirectionCollection::addMany()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\MissingUrlsRedirector\Entities;
6
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * Class     RedirectionCollection
12
 *
13
 * @package  Arcanedev\MissingUrlsRedirector\Entities
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class RedirectionCollection extends Collection
17
{
18
    /* -----------------------------------------------------------------
19
     |  Main Methods
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     * Make a new redirection.
25
     *
26
     * @param  string  $from
27
     * @param  string  $to
28
     * @param  int     $status
29
     *
30
     * @return \Arcanedev\MissingUrlsRedirector\Entities\Redirection
31
     */
32 72
    public function newRedirection(string $from, string $to, int $status): Redirection
33
    {
34 72
        return new Redirection($from, $to, $status);
35
    }
36
37
    /**
38
     * Add multiple redirections.
39
     *
40
     * @param  iterable  $from
41
     * @param  string    $to
42
     * @param  int       $status
43
     *
44
     * @return $this
45
     */
46 12
    public function addMany(iterable $from, string $to, int $status = RedirectResponse::HTTP_FOUND)
47
    {
48 12
        foreach ($from as $fromUrl => $fromStatus) {
49 12
            is_int($fromUrl)
50 12
                ? $this->addOne($fromStatus, $to, $status)
51 6
                : $this->addOne($fromUrl, $to, $fromStatus);
52
        }
53
54 12
        return $this;
55
    }
56
57
    /**
58
     * Add a new redirection.
59
     *
60
     * @param  string  $from
61
     * @param  string  $to
62
     * @param  int     $status
63
     *
64
     * @return $this
65
     */
66 72
    public function addOne(string $from, string $to, int $status = RedirectResponse::HTTP_MOVED_PERMANENTLY): self
67
    {
68 72
        $this->addRedirection(
69 72
            $this->newRedirection($from, $to, $status)
70
        );
71
72 72
        return $this;
73
    }
74
75
    /**
76
     * Add a redirection into the collection.
77
     *
78
     * @param  \Arcanedev\MissingUrlsRedirector\Entities\Redirection  $redirection
79
     *
80
     * @return $this
81
     */
82 84
    public function addRedirection(Redirection $redirection)
83
    {
84 84
        $key = $redirection->getTo();
85
86 84
        if ($this->has($key)) {
87
            // Update redirection if exists
88 42
            $redirections = $this->get($key, []);
89 42
            $redirections[$redirection->getFrom()] = $redirection;
90
        }
91
        else {
92
            // New redirection
93 84
            $redirections = [$redirection->getFrom() => $redirection];
94
        }
95
96 84
        return $this->put($key, $redirections);
97
    }
98
}
99