Completed
Push — master ( 2d40c2...8bc0f0 )
by Giancarlos
04:17
created

FeFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Administrador
5
 * Date: 20/07/2017
6
 * Time: 04:06 PM
7
 */
8
9
namespace Greenter\Factory;
10
11
use Greenter\Model\DocumentInterface;
12
use Greenter\Model\Response\BaseResult;
13
use Greenter\Security\SignedXml;
14
use Greenter\Validator\DocumentValidatorInterface;
15
use Greenter\Ws\Services\SenderInterface;
16
use Greenter\Xml\Builder\BuilderInterface;
17
use Greenter\Xml\Exception\ValidationException;
18
use Greenter\Zip\ZipFactory;
19
20
/**
21
 * Class FeFactory
22
 * @package Greenter\Factory
23
 */
24
class FeFactory implements FactoryInterface
25
{
26
    /**
27
     * @var SignedXml
28
     */
29
    private $signer;
30
31
    /**
32
     * @var ZipFactory
33
     */
34
    private $zipper;
35
36
    /**
37
     * @var SenderInterface
38
     */
39
    private $sender;
40
41
    /**
42
     * Ultimo xml generado.
43
     *
44
     * @var string
45
     */
46
    private $lastXml;
47
48
    /**
49
     * @var BuilderInterface
50
     */
51
    private $builder;
52
53
    /**
54
     * @var DocumentValidatorInterface
55
     */
56
    private $validator;
57
58
    /**
59
     * @param $validator
60
     * @return FeFactory
61
     */
62 68
    public function setValidator($validator)
63
    {
64 68
        $this->validator = $validator;
65 68
        return $this;
66
    }
67
68
    /**
69
     * BaseFactory constructor.
70
     */
71 74
    public function __construct()
72
    {
73 74
        $this->signer = new SignedXml();
74 74
        $this->zipper = new ZipFactory();
75 74
    }
76
77
    /**
78
     * @return BuilderInterface
79
     */
80 4
    public function getBuilder()
81
    {
82 4
        return $this->builder;
83
    }
84
85
    /**
86
     * @return SenderInterface
87
     */
88 4
    public function getSender()
89
    {
90 4
        return $this->sender;
91
    }
92
93
    /**
94
     * @param SenderInterface $sender
95
     * @return FeFactory
96
     */
97 68
    public function setSender($sender)
98
    {
99 68
        $this->sender = $sender;
100 68
        return $this;
101
    }
102
103
    /**
104
     * @param BuilderInterface $builder
105
     * @return FeFactory
106
     */
107 68
    public function setBuilder($builder)
108
    {
109 68
        $this->builder = $builder;
110 68
        return $this;
111
    }
112
113
    /**
114
     * @param DocumentInterface $document
115
     * @return BaseResult
116
     * @throws ValidationException
117
     */
118 68
    public function send(DocumentInterface $document)
119
    {
120 68
        $errs = $this->validator->validate($document);
121 68
        if (count($errs) > 0) {
122 30
            throw new ValidationException($errs);
123
        }
124 38
        $xml = $this->builder->build($document);
125 38
        $this->lastXml = $this->getXmmlSigned($xml);
126 38
        $filename = $document->getName();
127
128 38
        $zip = $this->zipper->compress("$filename.xml", $this->lastXml);
129 38
        return $this->sender->send("$filename.zip", $zip);
130
    }
131
132
    /**
133
     * Set Certicated content
134
     * @param string $cert
135
     */
136 70
    public function setCertificate($cert)
137
    {
138 70
        $this->signer->setCertificate($cert);
139 70
    }
140
141
    /**
142
     * Get Last XML Signed.
143
     *
144
     * @return string
145
     */
146 10
    public function getLastXml()
147
    {
148 10
        return $this->lastXml;
149
    }
150
151
    /**
152
     * @param string $xml
153
     * @return string
154
     */
155 38
    private function getXmmlSigned($xml)
156
    {
157 38
        return $this->signer->sign($xml);
158
    }
159
}