AbstractContentPart::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
3
/**
4
 * apparat-resource
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Resource
8
 * @subpackage Apparat\Resource\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Resource\Domain\Model\Part;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Resource\Domain\Model\Hydrator\AbstractSinglepartHydrator;
41
42
/**
43
 * Content part
44
 *
45
 * @package     Apparat\Resource
46
 * @subpackage Apparat\Resource\Domain
47
 */
48
abstract class AbstractContentPart extends AbstractPart
49
{
50
    /**
51
     * Media type
52
     *
53
     * @var string
54
     */
55
    const MEDIA_TYPE = 'application/octet-stream';
56
57
    /**
58
     * Text content
59
     *
60
     * @var string
61
     */
62
    protected $content = '';
63
64
    /**
65
     * Part constructor
66
     *
67
     * @param AbstractSinglepartHydrator $hydrator Associated hydrator
68
     * @param string $content Part content
69
     */
70 78
    public function __construct(AbstractSinglepartHydrator $hydrator, $content = '')
71
    {
72 78
        parent::__construct($hydrator);
73 78
        $this->content = $content;
74 78
    }
75
76
    /**
77
     * Serialize this file part
78
     *
79
     * @return string   File part content
80
     */
81 40
    public function __toString()
82
    {
83 40
        return strval($this->content);
84
    }
85
86
    /**
87
     * Return the media type of this part
88
     *
89
     * @return string   Media type
90
     */
91 4
    public function getMediaType()
92
    {
93 4
        return static::MEDIA_TYPE;
94
    }
95
96
    /**
97
     * Set the contents of a part
98
     *
99
     * @param string $data Contents
100
     * @param array $subparts Subparts
101
     * @return AbstractContentPart New content part
102
     */
103 19
    public function set($data, array $subparts = [])
104
    {
105 19
        unset($subparts);
106 19
        $class = get_class($this);
107 19
        return Kernel::create($class, [$this->hydrator, $data]);
108
    }
109
110
    /**
111
     * Return the part itself
112
     *
113
     * Content parts don't have subparts, so this method will always return the part itself
114
     *
115
     * @param array $subparts Subpart identifiers
116
     * @return PartInterface Self reference
117
     */
118 36
    public function get(array $subparts = [])
119
    {
120 36
        unset($subparts);
121 36
        return $this;
122
    }
123
124
    /**
125
     * Delegate a method call to a subpart
126
     *
127
     * @param string $method Method nae
128
     * @param array $subparts Subpart identifiers
129
     * @param array $arguments Method arguments
130
     * @return mixed Method result
131
     * @throws InvalidArgumentException If there are subpart identifiers
132
     */
133 30
    public function delegate($method, array $subparts, array $arguments)
134
    {
135
        // If there are subpart identifiers given
136 30
        if (count($subparts)) {
137 6
            throw new InvalidArgumentException(
138 6
                sprintf('Subparts are not allowed (%s)', implode('/', $subparts)),
139 6
                InvalidArgumentException::SUBPARTS_NOT_ALLOWED
140
            );
141
        }
142
143 24
        return parent::delegate($method, $subparts, $arguments);
144
    }
145
}
146