Completed
Push — email_refactor ( 765932 )
by Stefano
02:29
created

Email::makeEnvelope()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 54
Code Lines 40

Duplication

Lines 54
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 54
loc 54
rs 7.8331
cc 7
eloc 40
nc 36
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Email
5
 *
6
 * Send messages via Email services.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.it
11
 */
12
13
14
class Email {
15
  use Module;
16
17
  protected static $driver;
18
19
  protected static function agent(){
20
    return static::$driver;
21
  }
22
23
  public static function using($driver, $options = null){
24
    $class = '\\Email\\'.ucfirst(strtolower($driver));
25
    if ( ! class_exists($class) ) throw new \Exception("[Core.Email] : $driver driver not found.");
26
    $driver_service = Service::registerFactory('core.email',function() use ($class, $options){
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $driver_service is correct as \Service::registerFactor...ew $class($options); }) (which targets Service::registerFactory()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$driver_service is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27
      return new $class($options);
28
    });
29
    static::$driver = Service::{'core.email'}();
30
  }
31
32
  protected static function get_email_parts($value){
33
    if (strpos($value,'<')!==false){
34
      $value = str_replace('>','',$value);
35
      $parts = explode('<',$value,2);
36
      $name  = trim(current($parts));
37
      $email = trim(end($parts));
38
      return (object) [
39
        'name'  => $name,
40
        'email' => $email,
41
      ];
42
    } else {
43
      return (object) [
44
        'name'  => '',
45
        'email' => $value,
46
      ];
47
    }
48
  }
49
50 View Code Duplication
  public static function makeEnvelope(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    $uid = md5(uniqid(time()));
52
    $head = $body = [];
53
54
    if ($this->from)     $head[] = 'From: ' . $this->from;
0 ignored issues
show
Bug introduced by
The property from does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
55
    if ($this->replyTo)  $head[] = 'Reply-To: ' . $this->replyTo;
0 ignored issues
show
Bug introduced by
The property replyTo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
57
    $head[] = 'MIME-Version: 1.0';
58
    $head[] = "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
59
60
    $body[] = "--$uid";
61
    $body[] = "Content-type: text/html; charset=UTF-8";
62
    $body[] = "Content-Transfer-Encoding: quoted-printable";
63
    $body[] = '';
64
    $body[] = quoted_printable_encode($this->message);
0 ignored issues
show
Bug introduced by
The property message does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65
    $body[] = '';
66
67
68
    foreach ($this->attachments as $file) {
0 ignored issues
show
Bug introduced by
The property attachments does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
      if (is_string($file)) {
70
        $name = basename($file);
71
        $data = file_get_contents($file);
72
      } else {
73
        $name = $file['name'];
74
        $data = $file['content'];
75
      }
76
77
      $body[] = "--$uid";
78
      $body[] = "Content-type: application/octet-stream; name=\"".$name."\"";
79
      $body[] = "Content-Transfer-Encoding: base64";
80
      $body[] = "Content-Disposition: attachment; filename=\"".$name."\"";
81
      $body[] = '';
82
      $body[] = chunk_split(base64_encode($data));
83
      $body[] = '';
84
    }
85
86
    $body[] = "--$uid--";
87
88
    $success = true;
89
    $head    = implode("\r\n",$head);
90
    $body    = implode("\r\n",$body);
91
92
    foreach ($this->recipients as $to) {
0 ignored issues
show
Bug introduced by
The property recipients does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
93
      $current_success = mail(
94
           $to,
95
           $this->subject,
0 ignored issues
show
Bug introduced by
The property subject does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
96
           $body,
97
           $head
98
      );
99
      \Event::trigger('core.email.send', $to, $this->from, $this->subject, $body, $success);
100
      $success = $success && $current_success;
101
    }
102
    return $success;
103
  }
104
105
  protected static function send(array $options){
106
    $mail = static::agent();
107
108
    $options = array_merge([
109
      'to'          => '',
110
      'from'        => '',
111
      'replyTo'     => '',
112
      'subject'     => '',
113
      'message'     => '',
114
      'attachments' => [],
115
    ], $options);
116
117
    // To
118
    foreach ((array)$options['to'] as $value){
119
      $to = static::get_email_parts($value);
120
      empty($to->name)
121
        ? $mail->addAddress($to->email)
122
        : $mail->addAddress($to->email,$to->name);
123
    }
124
125
    // From
126
    $from = static::get_email_parts($options['from']);
127
    empty($from->name)
128
      ? $mail->from($from->email)
129
      : $mail->from($from->email,$from->name);
130
131
    // Reply
132
    $replyTo = static::get_email_parts($options['replyTo']);
133
    empty($replyTo->name)
134
      ? $mail->replyTo($replyTo->email)
135
      : $mail->replyTo($replyTo->email,$replyTo->name);
136
137
    // Subjects
138
    $mail->subject($options['subject']);
139
140
    // Message
141
    $mail->message($options['message']);
142
143
    // Attachments
144
    foreach ((array)$options['attachments'] as $value){
145
      $mail->addAttachment($value);
146
    }
147
148
    $result = $mail->send();
149
150
    static::$driver = Service::{'core.email'}();
151
152
    return $result;
153
  }
154
155
156
}
157
158
Email::using('native');
159