Passed
Push — main ( 614062...0598b7 )
by
unknown
02:44
created

UpdateMemberInMailMarketingListJob::setDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace MailMarketing\Jobs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Queue\ShouldQueue;
7
use Illuminate\Foundation\Bus\Dispatchable;
8
use Illuminate\Queue\InteractsWithQueue;
9
use Illuminate\Queue\SerializesModels;
10
use Illuminate\Support\Arr;
11
use MailMarketing\Drivers\MailMarketingInterface;
12
use MailMarketing\Entities\ResponseInterface;
13
use MailMarketing\Facades\MailMarketing;
14
15
class UpdateMemberInMailMarketingListJob implements ShouldQueue
16
{
17
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by MailMarketing\Jobs\Updat...rInMailMarketingListJob: $collectionClass, $id, $relations, $class, $keyBy
Loading history...
introduced by
The trait Illuminate\Queue\InteractsWithQueue requires some properties which are not provided by MailMarketing\Jobs\Updat...rInMailMarketingListJob: $failedWith, $releaseDelay
Loading history...
18
19
    public ?string $listId = null;
20
21
    public \ArrayAccess|array $member = [];
22
23
    public bool $isAdd = true;
24
25
    public ?string $driver = null;
26
27
    public array $tags;
28
29
    /**
30
     * AddMembersToMailMarketingList constructor.
31
     */
32 5
    public function __construct(?string $listId, \ArrayAccess|array $member, bool $isAdd = true, array $tags = [])
33
    {
34 5
        $this->listId = $listId;
35 5
        $this->member = $member;
36 5
        $this->isAdd  = $isAdd;
37 5
        $this->tags   = $tags;
38
    }
39
40
41 3
    public function handle(): ?ResponseInterface
42
    {
43 3
        if (!app()->environment(config('mail-marketing.marketing_jobs.envs'))) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        if (!app()->/** @scrutinizer ignore-call */ environment(config('mail-marketing.marketing_jobs.envs'))) {
Loading history...
44 1
            return null;
45
        }
46 2
        if ($this->isAdd) {
47 1
            return $this->addMemberToList();
48
        }
49
50 1
        return $this->removeMemberFromList();
51
    }
52
53 2
    public function setDriver(?string $driver): static
54
    {
55 2
        $this->driver = $driver;
56
57 2
        return $this;
58
    }
59
60 1
    protected function addMemberToList(): ResponseInterface
61
    {
62 1
        $driver = $this->driver();
63
64 1
        $response = $driver->addMemberToList(
65 1
            $this->listId,
66 1
            $this->member
67 1
        );
68
69 1
        if (count($this->tags)) {
70 1
            $driver->manageMemberTags(
71 1
                $this->listId,
72 1
                Arr::get($this->member, 'email'),
73 1
                $this->tags
74 1
            );
75
        }
76
77 1
        return $response;
78
    }
79
80 1
    protected function removeMemberFromList(): ResponseInterface
81
    {
82 1
        return $this->driver()->removeMemberFromList(
83 1
            $this->listId,
84 1
            $this->member
85 1
        );
86
    }
87
88 3
    public function driver(): MailMarketingInterface
89
    {
90 3
        return MailMarketing::driver($this->driver);
91
    }
92
}
93