MailableFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 47
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 10 1
A getArguments() 0 16 1
A setRecipient() 0 8 1
1
<?php
2
3
namespace Spatie\MailableTest;
4
5
use Illuminate\Mail\Mailable;
6
use ReflectionClass;
7
use ReflectionParameter;
8
9
class MailableFactory
10
{
11
    /** @var \Spatie\MailableTest\FakerArgumentValueProvider */
12
    protected $argumentValueProvider;
13
14
    public function __construct(ArgumentValueProvider $argumentValueProvider)
15
    {
16
        $this->argumentValueProvider = $argumentValueProvider;
0 ignored issues
show
Documentation Bug introduced by
$argumentValueProvider is of type object<Spatie\MailableTest\ArgumentValueProvider>, but the property $argumentValueProvider was declared to be of type object<Spatie\MailableTe...rArgumentValueProvider>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
17
    }
18
19
    public function getInstance(string $mailableClass, string $toEmail, $defaultValues): Mailable
20
    {
21
        $argumentValues = $this->getArguments($mailableClass, $defaultValues);
22
23
        $mailable = new $mailableClass(...$argumentValues);
24
25
        $mailable = $this->setRecipient($mailable, $toEmail);
26
27
        return $mailable;
28
    }
29
30
    public function getArguments(string $mailableClass, array $defaultValues)
31
    {
32
        $parameters = (new ReflectionClass($mailableClass))
33
            ->getConstructor()
34
            ->getParameters();
35
36
        return collect($parameters)
37
            ->map(function (ReflectionParameter $reflectionParameter) use ($mailableClass, $defaultValues) {
38
                return $this->argumentValueProvider->getValue(
39
                    $mailableClass,
40
                    $reflectionParameter->getName(),
41
                    $reflectionParameter->getType()->getName(),
42
                    $defaultValues[$reflectionParameter->getName()] ?? null
43
                );
44
            });
45
    }
46
47
    protected function setRecipient(Mailable $mailable, string $email): Mailable
48
    {
49
        $mailable->to($email);
50
        $mailable->cc([]);
51
        $mailable->bcc([]);
52
53
        return $mailable;
54
    }
55
}
56