Passed
Push — analysis-J2MpKa ( 6771ce )
by Philippe
12:39 queued 31s
created

UrlHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 10
eloc 27
c 4
b 3
f 0
dl 0
loc 82
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A allOnlineSingles() 0 3 1
A onlineModels() 0 3 1
B models() 0 31 6
A allOnlineModels() 0 5 1
A allModelsWithoutSelf() 0 5 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Urls;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use Thinktomorrow\Chief\Common\Helpers\Memoize;
8
use Thinktomorrow\Chief\Concerns\Morphable\Morphables;
9
use Thinktomorrow\Chief\FlatReferences\FlatReferencePresenter;
10
11
class UrlHelper
12
{
13
    /**
14
     * Internal api for fetching all models that have an active url.
15
     * This will return a grouped values array ready for select fields
16
     *
17
     * @param bool $onlySingles
18
     * @param Model|null $ignoredModel
19
     * @return array
20
     */
21
    public static function allOnlineModels(bool $onlySingles = false, Model $ignoredModel = null): array
22
    {
23
        $models = static::onlineModels($onlySingles, $ignoredModel);
24
25
        return FlatReferencePresenter::toGroupedSelectValues($models)->toArray();
26
    }
27
28
    /**
29
     * Internal api for fetching all models.
30
     * This will return a grouped values array ready for select fields
31
     *
32
     * @param bool $onlySingles
33
     * @param Model|null $ignoredModel
34
     * @return array
35
     */
36
    public static function allModelsWithoutSelf(Model $ignoredModel = null): array
37
    {
38
        $models = static::models(false, $ignoredModel, false);
39
40
        return FlatReferencePresenter::toGroupedSelectValues($models)->toArray();
41
    }
42
43
    public static function allOnlineSingles()
44
    {
45
        return static::allOnlineModels(true);
46
    }
47
48
    /**
49
     * Fetch all models that have an active url. Here we check on the ignored model
50
     * after retrieval from database so our memoized cache gets optimal usage.
51
     *
52
     * @param bool $onlySingles
53
     * @param Model|null $ignoredModel
54
     * @return Collection
55
     */
56
    public static function onlineModels(bool $onlySingles = false, Model $ignoredModel = null): Collection
57
    {
58
        return self::models($onlySingles, $ignoredModel, true);
59
    }
60
61
62
    public static function models(bool $onlySingles = false, Model $ignoredModel = null, $online = true)
63
    {
64
        $models = chiefMemoize('all-online-models', function () use ($onlySingles, $online) {
65
            $builder = UrlRecord::whereNull('redirect_id')
66
                ->select('model_type', 'model_id')
67
                ->groupBy('model_type', 'model_id');
68
69
            if ($onlySingles) {
70
                $builder->where('model_type', 'singles');
71
            }
72
73
            return $builder->get()->mapToGroups(function ($record) {
74
                return [$record->model_type => $record->model_id];
75
            })->map(function ($record, $key) {
76
                return Morphables::instance($key)->find($record->toArray());
77
            })->map->reject(function ($model) use ($online) {
78
                if ($online) {
79
                    return is_null($model) || !$model->isPublished();
80
                } // Invalid references to archived or removed models where url record still exists.
81
82
                return is_null($model);
83
            })->flatten();
84
        }, [$onlySingles]);
85
86
        if ($ignoredModel) {
87
            $models = $models->reject(function ($model) use ($ignoredModel) {
88
                return (get_class($model) === get_class($ignoredModel) && $model->id === $ignoredModel->id);
89
            });
90
        }
91
92
        return $models;
93
    }
94
}
95