Completed
Push — master ( 72613d...069c91 )
by Michael
02:16
created

extgalleryMailer   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 177
Duplicated Lines 5.65 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 10
loc 177
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A imageIncluded() 5 12 2
A imageLinked() 5 10 2
A send() 0 18 3
A assignTags() 0 12 1
A loadTemplate() 0 18 3
A setEcardId() 0 4 1
A setSubject() 0 4 1
A setToEmail() 0 4 1
A setToName() 0 4 1
A setFromEmail() 0 4 1
A setFromName() 0 4 1
A setGreetings() 0 4 1
A setDescription() 0 4 1
A setPhoto() 0 4 1

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * ExtGallery Class Manager
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   {@link https://xoops.org/ XOOPS Project}
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Zoullou (http://www.zoullou.net)
15
 * @package     ExtGallery
16
 */
17
18
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
19
20
require_once XOOPS_ROOT_PATH . '/class/mail/xoopsmultimailer.php';
21
22
/**
23
 * Class extgalleryMailer
24
 */
25
class extgalleryMailer
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
26
{
27
    public $mailer;
28
    public $type;
29
    public $tags = [];
30
31
    public $ecardId;
32
    public $subject;
33
    public $toEmail;
34
    public $toName;
35
    public $fromEmail;
36
    public $fromName;
37
    public $greetings;
38
    public $description;
39
    public $photo;
40
41
    /**
42
     * @param $type
43
     */
44
    public function __construct($type)
45
    {
46
        $this->mailer = new XoopsMultiMailer();
47
        $this->type   = $type;
48
    }
49
50
    public function imageIncluded()
51
    {
52 View Code Duplication
        if ('' === $this->photo->getVar('photo_serveur')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            $photoPath = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/medium/' . $this->photo->getVar('photo_name');
54
        } else {
55
            $photoPath = $this->photo->getVar('photo_serveur') . $this->photo->getVar('photo_name');
56
        }
57
        $this->tags['PHOTO_SRC'] = 'cid:photo';
58
        $this->tags['STAMP_SRC'] = 'cid:stamp';
59
        $this->mailer->addEmbeddedImage($photoPath, 'photo');
60
        $this->mailer->addEmbeddedImage(XOOPS_ROOT_PATH . '/modules/extgallery/assets/images/stamp.gif', 'stamp');
61
    }
62
63
    public function imageLinked()
64
    {
65 View Code Duplication
        if ('' == $this->photo->getVar('photo_serveur')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
            $photoUrl = XOOPS_URL . '/uploads/extgallery/public-photo/medium/' . $this->photo->getVar('photo_name');
67
        } else {
68
            $photoUrl = $this->photo->getVar('photo_serveur') . $this->photo->getVar('photo_name');
69
        }
70
        $this->tags['PHOTO_SRC'] = $photoUrl;
71
        $this->tags['STAMP_SRC'] = XOOPS_URL . '/modules/extgallery/assets/images/stamp.gif';
72
    }
73
74
    public function send()
75
    {
76
        $this->assignTags();
77
        if ('included' === $this->type) {
78
            $this->imageIncluded();
79
        } elseif ('linked' === $this->type) {
80
            $this->imageLinked();
81
        }
82
83
        $this->mailer->From     = $this->fromEmail;
84
        $this->mailer->FromName = $this->fromName;
85
        $this->mailer->Subject  = $this->subject;
86
        $this->mailer->Body     = $this->loadTemplate('ecard_html.tpl');
87
        $this->mailer->AltBody  = $this->loadTemplate('ecard_text.tpl');
88
        $this->mailer->addAddress($this->toEmail, $this->toName);
89
        //$this->mailer->AddReplyTo($this->fromEmail, $this->fromName);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
        $this->mailer->send();
91
    }
92
93
    public function assignTags()
0 ignored issues
show
Coding Style introduced by
assignTags uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
94
    {
95
        $this->tags['ECARD_LINK']  = XOOPS_URL . '/modules/extgallery/public-viewecard.php?id=' . $this->ecardId;
96
        $this->tags['EXP_EMAIL']   = $this->fromEmail;
97
        $this->tags['EXP_NAME']    = $this->fromName;
98
        $this->tags['REC_NAME']    = $this->toName;
99
        $this->tags['GREETINGS']   = $this->greetings;
100
        $this->tags['DESCRIPTION'] = $this->description;
101
        $this->tags['MODULE_LINK'] = XOOPS_URL . '/modules/extgallery/';
102
        $this->tags['SITE_NAME']   = $GLOBALS['xoopsConfig']['sitename'];
103
        $this->tags['SITE_URL']    = XOOPS_URL;
104
    }
105
106
    /**
107
     * @param $name
108
     *
109
     * @return mixed|string
110
     */
111
    public function loadTemplate($name)
112
    {
113
        global $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
114
115
        if (file_exists(XOOPS_ROOT_PATH . '/modules/extgallery/language/' . $xoopsConfig['language'] . '/mail_template/' . $name)) {
116
            $path = XOOPS_ROOT_PATH . '/modules/extgallery/language/' . $xoopsConfig['language'] . '/mail_template/' . $name;
117
        } else {
118
            $path = XOOPS_ROOT_PATH . '/modules/extgallery/language/english/mail_template/' . $name;
119
        }
120
        $fd   = @fopen($path, 'r');
121
        $body = fread($fd, filesize($path));
122
        // replace tags with actual values
123
        foreach ($this->tags as $k => $v) {
124
            $body = str_replace('{' . $k . '}', $v, $body);
125
        }
126
127
        return $body;
128
    }
129
130
    /**
131
     * @param $ecardId
132
     */
133
    public function setEcardId($ecardId)
134
    {
135
        $this->ecardId = $ecardId;
136
    }
137
138
    /**
139
     * @param $subject
140
     */
141
    public function setSubject($subject)
142
    {
143
        $this->subject = $subject;
144
    }
145
146
    /**
147
     * @param $email
148
     */
149
    public function setToEmail($email)
150
    {
151
        $this->toEmail = $email;
152
    }
153
154
    /**
155
     * @param $name
156
     */
157
    public function setToName($name)
158
    {
159
        $this->toName = $name;
160
    }
161
162
    /**
163
     * @param $email
164
     */
165
    public function setFromEmail($email)
166
    {
167
        $this->fromEmail = $email;
168
    }
169
170
    /**
171
     * @param $name
172
     */
173
    public function setFromName($name)
174
    {
175
        $this->fromName = $name;
176
    }
177
178
    /**
179
     * @param $greetings
180
     */
181
    public function setGreetings($greetings)
182
    {
183
        $this->greetings = $greetings;
184
    }
185
186
    /**
187
     * @param $description
188
     */
189
    public function setDescription($description)
190
    {
191
        $this->description = $description;
192
    }
193
194
    /**
195
     * @param $photo
196
     */
197
    public function setPhoto($photo)
198
    {
199
        $this->photo = $photo;
200
    }
201
}
202