Issues (112)

src/FileAdder/Traits/HasSubjectTrait.php (1 issue)

1
<?php
2
3
namespace ByTIC\MediaLibrary\FileAdder\Traits;
4
5
use ByTIC\MediaLibrary\Exceptions\RuntimeException;
6
use ByTIC\MediaLibrary\FileAdder\FileAdder;
7
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
8
use ByTIC\MediaLibrary\HasMedia\Interfaces\HasMedia;
9
use Nip\Utility\Oop;
10
11
/**
12
 * Trait HasSubjectTrait.
13
 */
14
trait HasSubjectTrait
15
{
16
    /** @var HasMediaTrait|HasMedia subject */
17
    protected $subject;
18
19
    /**
20 3
     * @return HasMediaTrait|HasMedia
21
     */
22 3
    public function getSubject(): HasMedia
23
    {
24
        return $this->subject;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->subject could return the type ByTIC\MediaLibrary\HasMedia\HasMediaTrait which is incompatible with the type-hinted return ByTIC\MediaLibrary\HasMedia\Interfaces\HasMedia. Consider adding an additional type-check to rule them out.
Loading history...
25
    }
26
27
    /**
28 2
     * @return bool
29
     */
30 2
    public function hasSubject(): bool
31
    {
32
        if (!is_object($this->subject)) {
33
            return false;
34
        }
35
        $hasInterface = $this->subject instanceof HasMedia;
36
        if ($hasInterface) {
37
            return true;
38 3
        }
39
        if (Oop::classUsesTrait($this->subject, HasMediaTrait::class)) {
40 3
            throw new RuntimeException("Class " . get_class($this->subject) . " does not have the HasMedia interface");
41
        }
42 3
        return false;
43
    }
44
45
    /**
46
     * @param HasMediaTrait $subject
47
     *
48
     * @return FileAdder|self
49
     */
50
    public function setSubject($subject)
51
    {
52
        $this->subject = $subject;
53
54
        return $this;
55
    }
56
}
57