Passed
Push — states-and-image-validation ( a71210...2ab511 )
by Ben
57:13 queued 47:25
created

UrlHelper::allOnlineSingles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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