Passed
Push — master ( 240c83...03f39b )
by Mathias
06:41
created

Forward::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright https://yawik.org/COPYRIGHT.php
7
 * @license   MIT
8
 */
9
10
/** Forward.php */
11
12
namespace Applications\Mail;
13
14
use Applications\Entity\Application;
15
use Core\Mail\TranslatorAwareMessage;
16
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
17
use Laminas\Mime;
18
use Laminas\View\HelperPluginManager;
19
20
/**
21
* Sends an e-mail containing an applications
22
*/
23
class Forward extends TranslatorAwareMessage
24
{
25
    /**
26
     * @var Application
27
     */
28
    protected $application;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $isInitialized = false;
34
35
    protected $viewManager;
36
37
    protected $fileRepository;
38
39
40
    public function __construct(HelperPluginManager $viewHelperManager, DocumentRepository $attachmentRepository, array $options = [])
41
    {
42
        $this->viewManager = $viewHelperManager;
43
        $this->fileRepository = $attachmentRepository;
44
        parent::__construct($options);
45
    }
46
47
    /**
48
     * @param $application
49
     * @return $this
50
     */
51
    public function setApplication(Application $application)
52
    {
53
        $this->application = $application;
54
        if ($this->isInitialized) {
55
            $this->generateBody();
56
        }
57
        return $this;
58
    }
59
60
    public function init()
61
    {
62
        $this->isInitialized = true;
63
        if (!$this->application) {
64
            return;
65
        }
66
        $this->setEncoding('UTF-8');
67
        $subject = /* @translate */ 'Fwd: Application to "%s" dated %s';
68
        if ($this->isTranslatorEnabled()) {
69
            $subject = $this->getTranslator()->translate($subject);
70
        }
71
        $this->setSubject(
72
            sprintf(
73
                $subject,
74
                $this->application->getJob()->getTitle(),
75
                strftime('%x', $this->application->getDateCreated()->getTimestamp())
76
            )
77
        );
78
        $this->generateBody();
79
    }
80
81
    /**
82
     * Generates the Mail Body
83
     */
84
    protected function generateBody()
85
    {
86
        $message = new Mime\Message();
87
88
        $text = $this->generateHtml();
89
        $textPart = new Mime\Part($text);
90
        $textPart->type = 'text/html';
91
        $textPart->charset = 'UTF-8';
92
        $textPart->disposition = Mime\Mime::DISPOSITION_INLINE;
93
        $message->addPart($textPart);
94
95
        if (is_object($this->application->getContact()->getImage()) &&
96
            $this->application->getContact()->getImage()->getId()) {
97
            /* @var $image \Auth\Entity\UserImage */
98
            $image = $this->application->getContact()->getImage();
99
            $part = new Mime\Part($image->getResource());
0 ignored issues
show
Bug introduced by
The method getResource() does not exist on Auth\Entity\UserImage. ( Ignorable by Annotation )

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

99
            $part = new Mime\Part($image->/** @scrutinizer ignore-call */ getResource());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
            $part->setType($image->getType());
0 ignored issues
show
Bug introduced by
The method getType() does not exist on Auth\Entity\UserImage. ( Ignorable by Annotation )

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

100
            $part->setType($image->/** @scrutinizer ignore-call */ getType());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            $part->setEncoding(Mime\Mime::ENCODING_BASE64);
102
            $part->setFileName($image->getName());
103
            $part->setDisposition(Mime\Mime::DISPOSITION_ATTACHMENT);
104
            $message->addPart($part);
105
        }
106
107
        foreach ($this->application->getAttachments() as $attachment) {
108
            /** @var \Applications\Entity\Attachment $attachment */
109
            $stream = $this->fileRepository->openDownloadStream($attachment->getId());
0 ignored issues
show
Bug introduced by
The method openDownloadStream() does not exist on Doctrine\ODM\MongoDB\Repository\DocumentRepository. It seems like you code against a sub-type of Doctrine\ODM\MongoDB\Repository\DocumentRepository such as Doctrine\ODM\MongoDB\Rep...DefaultGridFSRepository. ( Ignorable by Annotation )

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

109
            /** @scrutinizer ignore-call */ 
110
            $stream = $this->fileRepository->openDownloadStream($attachment->getId());
Loading history...
110
            $part = new Mime\Part($stream);
111
            $part->setType($attachment->getMetadata()->getContentType());
112
            $part->encoding = Mime\Mime::ENCODING_BASE64;
113
            $part->filename = $attachment->getMetadata()->getName();
114
            $part->disposition = Mime\Mime::DISPOSITION_ATTACHMENT;
115
            $message->addPart($part);
116
        }
117
118
        $this->setBody($message);
119
    }
120
121
    /**
122
     * Generates a mail containing an Application.
123
     *
124
     * @return mixed
125
     */
126
    protected function generateHtml()
127
    {
128
        /*
129
         * "ViewHelperManager" defined by ZF2
130
         *  see http://framework.zend.com/manual/2.0/en/modules/zend.mvc.services.html#viewmanager
131
         */
132
        $viewManager = $this->viewManager;
133
134
        return $viewManager->get("partial")->__invoke('applications/mail/forward', array("application"=>$this->application));
135
    }
136
}
137