Issues (61)

src/Models/Owners/Owner.php (4 issues)

1
<?php
2
3
namespace Itstructure\MFU\Models\Owners;
4
5
use Illuminate\Database\Query\Builder as QueryBuilder;
6
use Illuminate\Database\Eloquent\{Collection, Model, Builder as EloquentBuilder};
7
8
/**
9
 * Class Owner
10
 * @package Itstructure\MFU\Models\Owners
11
 */
12
abstract class Owner extends Model
13
{
14
    /**
15
     * Get model (mediafile/album) primary key name.
16
     * @return string
17
     */
18
    abstract protected static function getDependencyKeyName(): string;
19
20
    /**
21
     * Remove:
22
     * 1. Mediafile entry with physical directories and files when album is removed.
23
     * 2. Album entry with related mediafiles and physical directories and files when parent entity (Product, Post, Page e.t.c) is removed.
24
     * @param int $entityId
25
     * @return bool
26
     */
27
    abstract protected static function removeDependency(int $entityId): bool;
28
29
    /**
30
     * Add owner to mediafiles table.
31
     * @param int    $modelId
32
     * @param int    $ownerId
33
     * @param string $ownerName
34
     * @param string $ownerAttribute
35
     * @return bool
36
     */
37
    public static function addOwner(int $modelId, int $ownerId, string $ownerName, string $ownerAttribute): bool
38
    {
39
        $ownerModel = new static();
40
        $ownerModel->{static::getDependencyKeyName()} = $modelId;
41
        $ownerModel->owner_id = $ownerId;
0 ignored issues
show
The property owner_id does not seem to exist on Itstructure\MFU\Models\Owners\Owner. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
42
        $ownerModel->owner_name = $ownerName;
0 ignored issues
show
The property owner_name does not seem to exist on Itstructure\MFU\Models\Owners\Owner. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
43
        $ownerModel->owner_attribute = $ownerAttribute;
0 ignored issues
show
The property owner_attribute does not seem to exist on Itstructure\MFU\Models\Owners\Owner. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
44
45
        return $ownerModel->save();
46
    }
47
48
    /**
49
     * Remove this mediafile/album owner.
50
     * @param int $ownerId
51
     * @param string $ownerName
52
     * @param string|null $ownerAttribute
53
     * @param bool $removeDependencies
54
     * @return bool
55
     */
56
    public static function removeOwner(int $ownerId, string $ownerName, string $ownerAttribute = null, bool $removeDependencies = false): bool
57
    {
58
        $query = static::query();
59
        foreach (static::buildFilterOptions($ownerId, $ownerName, $ownerAttribute) as $attribute => $value) {
60
            /* @var QueryBuilder $q */
61
            $query->where($attribute, '=', $value);
62
        }
63
        $entries = $query->get();
64
        $thisOwnerEntityIds = $entries->pluck(static::getDependencyKeyName())->toArray();
65
        $otherOwnerEntityIds = static::filterMultipliedEntityIds($ownerId, $ownerName, $thisOwnerEntityIds)
66
            ->pluck(static::getDependencyKeyName())
67
            ->toArray();
68
        $deleted = 0;
69
        foreach ($entries as $entry) {
70
            $entityId = $entry->{static::getDependencyKeyName()};
71
            $entry->delete();
72
            if ($removeDependencies && !in_array($entityId, $otherOwnerEntityIds)) {
73
                static::removeDependency($entityId);
74
            }
75
            $deleted++;
76
        }
77
        return $deleted > 0;
78
    }
79
80
    /**
81
     * @return int
82
     */
83
    public function getOwnerId(): int
84
    {
85
        return $this->owner_id;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getOwnerName(): string
92
    {
93
        return $this->owner_name;
94
    }
95
96
    /**
97
     * Getting entity id's which are related with Other owners too.
98
     * @param int $ownerId
99
     * @param string $ownerName
100
     * @param array $entityIds
101
     * @return Collection|static[]
102
     */
103
    public static function filterMultipliedEntityIds(int $ownerId, string $ownerName, array $entityIds): Collection
104
    {
105
        return static::query()->select(static::getDependencyKeyName())
106
            ->whereIn(static::getDependencyKeyName(), $entityIds)
107
            ->where(function ($q) use ($ownerId, $ownerName) {
108
                /* @var QueryBuilder $q */
109
                $q->where('owner_id', '!=', $ownerId)
110
                    ->orWhere('owner_name', '!=', $ownerName);
111
            })
112
            ->get();
113
    }
114
115
    /**
116
     * Get Id's by owner.
117
     * @param string $nameId
118
     * @param array $args It can be an array of the next params: owner_name{string}, owner_id{int}, owner_attribute{string}.
119
     * @return EloquentBuilder
120
     */
121
    protected static function getEntityIdsQuery(string $nameId, array $args): EloquentBuilder
122
    {
123
        return static::query()->select($nameId)->when(count($args) > 0, function($q) use ($args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::query()->...ion(...) { /* ... */ }) could return the type Itstructure\MFU\Models\Owners\Owner which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
124
            foreach ($args as $attribute => $value) {
125
                /* @var QueryBuilder $q */
126
                $q->where($attribute, $value);
127
            }
128
        });
129
    }
130
131
    /**
132
     * Build filter options for some actions.
133
     * @param int $ownerId
134
     * @param string $ownerName
135
     * @param string|null $ownerAttribute
136
     * @return array
137
     */
138
    protected static function buildFilterOptions(int $ownerId, string $ownerName, string $ownerAttribute = null): array
139
    {
140
        return array_merge([
141
            'owner_id' => $ownerId,
142
            'owner_name' => $ownerName
143
        ], empty($ownerAttribute) ? [] : ['owner_attribute' => $ownerAttribute]);
144
    }
145
}
146