Completed
Push — master ( c83f11...e27db7 )
by
unknown
01:42
created

MailTemplate::findForMailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Spatie\MailTemplates\Models;
4
5
use Illuminate\Contracts\Mail\Mailable;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Spatie\MailTemplates\Exceptions\MissingMailTemplate;
9
use Spatie\MailTemplates\Interfaces\MailTemplateInterface;
10
11
class MailTemplate extends Model implements MailTemplateInterface
12
{
13
    protected $guarded = [];
14
15
    public function scopeForMailable(Builder $query, Mailable $mailable): Builder
16
    {
17
        return $query->where('mailable', get_class($mailable));
18
    }
19
20
    public static function findForMailable(Mailable $mailable): self
21
    {
22
        $mailTemplate = static::forMailable($mailable)->first();
0 ignored issues
show
Bug introduced by
The method forMailable() does not exist on Spatie\MailTemplates\Models\MailTemplate. Did you maybe mean scopeForMailable()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
23
24
        if (! $mailTemplate) {
25
            throw MissingMailTemplate::forMailable($mailable);
26
        }
27
28
        return $mailTemplate;
29
    }
30
31
    public function getHtmlLayout(): ?string
32
    {
33
        return null;
34
    }
35
36
    public function getTextLayout(): ?string
37
    {
38
        return null;
39
    }
40
41
    public function getVariables(): array
42
    {
43
        $mailableClass = $this->mailable;
0 ignored issues
show
Documentation introduced by
The property mailable does not exist on object<Spatie\MailTemplates\Models\MailTemplate>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
45
        if (! class_exists($mailableClass)) {
46
            return [];
47
        }
48
49
        return $mailableClass::getVariables();
50
    }
51
52
    public function getVariablesAttribute(): array
53
    {
54
        return $this->getVariables();
55
    }
56
57
    public function subject(): string
58
    {
59
        return $this->subject;
0 ignored issues
show
Documentation introduced by
The property subject does not exist on object<Spatie\MailTemplates\Models\MailTemplate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
    }
61
62
    public function htmlTemplate(): string
63
    {
64
        return $this->template;
0 ignored issues
show
Documentation introduced by
The property template does not exist on object<Spatie\MailTemplates\Models\MailTemplate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
    }
66
67
    public function textTemplate(): string
68
    {
69
        return $this->template;
0 ignored issues
show
Documentation introduced by
The property template does not exist on object<Spatie\MailTemplates\Models\MailTemplate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
    }
71
}
72