SendMailAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 14.61 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 3
dl 13
loc 89
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A execute() 0 17 2
A getSocketValue() 0 15 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * NextFlow (http://github.com/nextflow)
4
 *
5
 * @link http://github.com/nextflow/nextflow-php for the canonical source repository
6
 * @copyright Copyright (c) 2014-2016 NextFlow (http://github.com/nextflow)
7
 * @license https://raw.github.com/nextflow/nextflow-php/master/LICENSE MIT
8
 */
9
10
namespace NextFlow\Mail\Action;
11
12
use InvalidArgumentException;
13
use NextFlow\Core\Action\AbstractAction;
14
use NextFlow\Mail\Transport\TransportInterface;
15
use RuntimeException;
16
17
/**
18
 * An action that makes it possible to send an e-mail.
19
 */
20
final class SendMailAction extends AbstractAction
21
{
22
    /** The socket that is activated when this action is successful. */
23
    const SOCKET_OUTPUT_SUCCESS = 'success';
24
25
    /** The socket that is activated when this action is not successful. */
26
    const SOCKET_OUTPUT_ERROR = 'error';
27
28
    /** The socket that contains the addresses to send to. */
29
    const SOCKET_TO = 'to';
30
31
    /** The socket that contains the address to send to. */
32
    const SOCKET_SUBJECT = 'subject';
33
34
    /** The socket that contains the message to send. */
35
    const SOCKET_MESSAGE = 'message';
36
37
    /** The socket that contains the from address. */
38
    const SOCKET_FROM = 'from';
39
40
    /**
41
     * @var TransportInterface
42
     */
43
    private $transport;
44
45
    /**
46
     * Initializes a new instance of this class.
47
     *
48
     * @param TransportInterface $transport The mail transport used to send email messages.
49
     */
50 View Code Duplication
    public function __construct(TransportInterface $transport)
51
    {
52
        parent::__construct();
53
54
        $this->createSocket(self::SOCKET_OUTPUT_SUCCESS);
55
        $this->createSocket(self::SOCKET_OUTPUT_ERROR);
56
        $this->createSocket(self::SOCKET_TO);
57
        $this->createSocket(self::SOCKET_SUBJECT);
58
        $this->createSocket(self::SOCKET_MESSAGE);
59
        $this->createSocket(self::SOCKET_FROM);
60
61
        $this->transport = $transport;
62
    }
63
64
    /**
65
     * Executes the node's logic.
66
     */
67
    public function execute()
68
    {
69
        $params = [
70
            'to' => $this->getSocketValue(self::SOCKET_TO, 'No address to send an e-mail to.'),
71
            'subject' => $this->getSocketValue(self::SOCKET_SUBJECT, 'There is no subject set.'),
72
            'message' => $this->getSocketValue(self::SOCKET_MESSAGE, 'There is no message to send.'),
73
            'from' => $this->getSocketValue(self::SOCKET_FROM, 'There is no from address set.'),
74
        ];
75
76
        $result = $this->transport->send($params);
77
78
        if ($result) {
79
            $this->activate(self::SOCKET_OUTPUT_SUCCESS);
80
        } else {
81
            $this->activate(self::SOCKET_OUTPUT_ERROR);
82
        }
83
    }
84
85
    /**
86
     * Gets the value of the given socket.
87
     *
88
     * @param string $socketName The name of the socket to get the value for.
89
     * @param string $message The message of the exception that is thrown in case of an error.
90
     * @return mixed
91
     * @throws RuntimeException Thrown when there are no nodes.
92
     */
93
    private function getSocketValue($socketName, $message)
94
    {
95
        $socket = $this->getSocket($socketName);
96
        if (!$socket || !$socket->hasNodes()) {
97
            throw new RuntimeException(sprintf('The socket "%s" has no nodes.', $socketName));
98
        }
99
100
        $value = $socket->getNode(0)->getValue();
101
102
        if ($value === null) {
103
            throw new InvalidArgumentException($message);
104
        }
105
106
        return $value;
107
    }
108
}
109