MailHandler::send()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 14
nc 8
nop 1
1
<?php
2
3
namespace yiicod\mailqueue\components;
4
5
use Yii;
6
use yiicod\mailqueue\models\MailRepositoryInterface;
7
8
class MailHandler implements MailHandlerInterface
9
{
10
    const STATUS_SENT = 1;
11
12
    const STATUS_PENDING = 0;
13
14
    const STATUS_FAILED = 2;
15
16
    /**
17
     * Find mails
18
     *
19
     * @return mixed
20
     */
21
    public function findAll()
22
    {
23
        $class = Yii::$app->get('mailqueue')->modelMap['mailQueue']['class'];
24
        $models = $class::find()
25
            ->where(sprintf('%s=:pending', $class::attributesMap()['fieldStatus']))
26
            ->params([':pending' => self::STATUS_PENDING])
27
            ->orderBy($class::attributesMap()['fieldPriority'])
28
            ->limit(60)
29
            ->all();
30
31
        return $models;
32
    }
33
34
    /**
35
     * @param MailRepositoryInterface $item
36
     *
37
     * @return bool
38
     */
39
    public function send(MailRepositoryInterface $item): bool
40
    {
41
        if (Yii::$app->has($item->mailer ?? '')) {
0 ignored issues
show
Bug introduced by
Accessing mailer on the interface yiicod\mailqueue\models\MailRepositoryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
42
            $mailer = Yii::$app->get($item->mailer);
0 ignored issues
show
Bug introduced by
Accessing mailer on the interface yiicod\mailqueue\models\MailRepositoryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
43
        } else {
44
            $mailer = Yii::$app->mailer;
45
        }
46
        $message = $mailer->compose(null, ['item' => $item]);
47
        $message->setTo($item->to)
0 ignored issues
show
Bug introduced by
Accessing to on the interface yiicod\mailqueue\models\MailRepositoryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
48
            ->setSubject($item->subject)
0 ignored issues
show
Bug introduced by
Accessing subject on the interface yiicod\mailqueue\models\MailRepositoryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49
            ->setHtmlBody($item->body);
0 ignored issues
show
Bug introduced by
Accessing body on the interface yiicod\mailqueue\models\MailRepositoryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
50
        if (false === empty($item->from) && true === empty($message->getFrom())) {
0 ignored issues
show
Bug introduced by
Accessing from on the interface yiicod\mailqueue\models\MailRepositoryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
51
            $message->setFrom($item->from);
0 ignored issues
show
Bug introduced by
Accessing from on the interface yiicod\mailqueue\models\MailRepositoryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
52
        }
53
        foreach ($item->getAttaches() as $attach) {
54
            $message->attach($attach);
55
        }
56
57
        return $message->send();
58
    }
59
60
    /**
61
     * @param array $ids
62
     *
63
     * @return mixed
64
     */
65
    public function success(array $ids)
66
    {
67
        $class = Yii::$app->get('mailqueue')->modelMap['mailQueue']['class'];
68
        $class::updateAll([
69
            $class::attributesMap()['fieldStatus'] => self::STATUS_SENT,
70
            $class::attributesMap()['fieldCreatedDate'] => date('Y-m-d H:i:s'),
71
        ], ['id' => $ids]);
72
    }
73
74
    /**
75
     * @param array $ids
76
     *
77
     * @return mixed
78
     */
79
    public function failed(array $ids)
80
    {
81
        $class = Yii::$app->get('mailqueue')->modelMap['mailQueue']['class'];
82
        $class::updateAll([
83
            $class::attributesMap()['fieldStatus'] => self::STATUS_FAILED,
84
            $class::attributesMap()['fieldCreatedDate'] => date('Y-m-d H:i:s'),
85
        ], ['id' => $ids]);
86
    }
87
}
88