CachingUrlRewriteRepository::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RuthgerIdema\UrlRewrite\Repositories\Decorators;
6
7
use Illuminate\Cache\TaggableStore;
8
use Illuminate\Contracts\Cache\Repository as Cache;
9
use RuthgerIdema\UrlRewrite\Repositories\Interfaces\UrlRewriteInterface;
10
11
class CachingUrlRewriteRepository implements UrlRewriteInterface
12
{
13
    /** @var string */
14
    public const URL_REWRITE_ALL = 'url_rewrite_all';
15
16
    /** @var string */
17
    public const URL_REWRITE_ID = 'url_rewrite_id_';
18
19
    /** @var string */
20
    public const URL_REWRITE_REQUEST_PATH = 'url_rewrite_request_path_';
21
22
    /** @var string */
23
    public const URL_REWRITE_TARGET_PATH = 'url_rewrite_target_path_';
24
25
    /** @var string */
26
    public const URL_REWRITE_TYPE_ATTRIBUTES = 'url_rewrite_type_attributes_';
27
28
    /** @var UrlRewriteInterface */
29
    protected $repository;
30
31
    /** @var Cache */
32
    protected $cache;
33
34
    public function __construct(
35
        UrlRewriteInterface $repository,
36
        Cache $cache
37
    ) {
38
        $this->repository = $repository;
39
        $this->cache = $cache;
40
        $this->addTagIfPossible();
41
    }
42
43
    protected function remember(string $key, string $method, ...$arguments)
44
    {
45
        return $this->cache->remember(
46
            $key,
47
            $this->getTtl(),
48
            function () use ($method, $arguments) {
49
                return $this->repository->{$method}(...$arguments);
50
            }
51
        );
52
    }
53
54
    protected function addTagIfPossible(): void
55
    {
56
        if ($this->cache->getStore() instanceof TaggableStore) {
57
            $this->cache = $this->cache->tags(config('url-rewrite.cache-key'));
58
        }
59
    }
60
61
    protected function getTtl(): int
62
    {
63
        return config('url-rewrite.cache-ttl');
64
    }
65
66
    public function find(int $id): ?object
67
    {
68
        return $this->remember(self::URL_REWRITE_ID.$id, __FUNCTION__, $id);
69
    }
70
71
    public function getByRequestPath(string $url): ?object
72
    {
73
        return $this->remember(static::URL_REWRITE_REQUEST_PATH.md5($url), __FUNCTION__, $url);
74
    }
75
76
    public function all(): ?object
77
    {
78
        return $this->remember(static::URL_REWRITE_ALL, __FUNCTION__);
79
    }
80
81
    public function getByTargetPath(string $url): ?object
82
    {
83
        return $this->remember(static::URL_REWRITE_TARGET_PATH.md5($url), __FUNCTION__, $url);
84
    }
85
86
    public function getByTypeAndAttributes(string $type, array $attributes): ?object
87
    {
88
        return $this->remember(
89
            self::URL_REWRITE_TYPE_ATTRIBUTES.md5($type.json_encode($attributes)),
90
            __FUNCTION__,
91
            $type,
92
            $attributes
93
        );
94
    }
95
96
    public function getModel(): object
97
    {
98
        return $this->repository->getModel();
99
    }
100
101
    public function setModel(object $model): object
102
    {
103
        return $this->repository->setModel($model);
104
    }
105
106
    public function delete(int $id): bool
107
    {
108
        $deleted = $this->repository->delete($id);
109
110
        $this->forgetById($id);
111
112
        return $deleted;
113
    }
114
115
    protected function forgetById(int $id): void
116
    {
117
        if ($model = $this->find($id)) {
118
            $this->cache->forget(static::URL_REWRITE_ALL);
119
            $this->cache->forget(static::URL_REWRITE_ID.$model->id);
120
            $this->cache->forget(static::URL_REWRITE_REQUEST_PATH.md5($model->request_path));
121
            $this->cache->forget(static::URL_REWRITE_TARGET_PATH.md5($model->target_path));
122
            $this->cache->forget(static::URL_REWRITE_TYPE_ATTRIBUTES.md5($model->type.json_encode($model->type_attributes)));
123
        }
124
    }
125
126
    public function create(
127
        string $requestPath,
128
        ?string $targetPath,
129
        ?string $type = null,
130
        ?array $typeAttributes = null,
131
        int $redirectType = 0,
132
        ?string $description = null,
133
        ?bool $unique = false
134
    ): object {
135
        return $this->repository->create(
136
            $requestPath,
137
            $targetPath,
138
            $type,
139
            $typeAttributes,
140
            $redirectType,
141
            $description,
142
            $unique
143
        );
144
    }
145
146
    public function update(array $data, int $id): object
147
    {
148
        $updated = $this->repository->update($data, $id);
149
150
        $this->forgetById($id);
151
152
        return $updated;
153
    }
154
155
    public function regenerateRoute($urlRewrite): object
156
    {
157
        return $this->repository->regenerateRoute($urlRewrite);
158
    }
159
}
160