ResourceTrait::toTarget()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * apparat/resource
5
 *
6
 * @category    Jkphl
7
 * @package     Apparat\Resource
8
 * @subpackage  Apparat\Resource\Infrastructure
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
/***********************************************************************************
16
 *  The MIT License (MIT)
17
 *
18
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
19
 *
20
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
21
 *  this software and associated documentation files (the "Software"), to deal in
22
 *  the Software without restriction, including without limitation the rights to
23
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
24
 *  the Software, and to permit persons to whom the Software is furnished to do so,
25
 *  subject to the following conditions:
26
 *
27
 *  The above copyright notice and this permission notice shall be included in all
28
 *  copies or substantial portions of the Software.
29
 *
30
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
32
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
33
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
34
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36
 ***********************************************************************************/
37
38
namespace Apparat\Resource\Infrastructure\Model\Resource;
39
40
use Apparat\Kernel\Ports\Kernel;
41
use Apparat\Resource\Domain\Contract\WriterInterface;
42
use Apparat\Resource\Domain\Model\Resource\AbstractResource;
43
use Apparat\Resource\Infrastructure\Io\InMemory\Writer as InMemoryWriter;
44
use Apparat\Resource\Ports\InvalidArgumentException;
45
use Apparat\Resource\Ports\InvalidWriterArgumentException;
46
use Apparat\Resource\Ports\Tools;
47
48
/**
49
 * Resource factory methods
50
 *
51
 * @package     Apparat\Resource
52
 * @subpackage  Apparat\Resource\Infrastructure
53
 */
54
trait ResourceTrait
55
{
56
    /*******************************************************************************
57
     * PUBLIC METHODS
58
     *******************************************************************************/
59
60
    /**
61
     * String serialization
62
     *
63
     * @return string String value (file content)
64
     */
65 3
    public function __toString()
66
    {
67
        /** @var InMemoryWriter $writer */
68 3
        $writer = Kernel::create(InMemoryWriter::class);
69
70
        /** @var AbstractResource $this */
71 3
        $this->dump($writer);
72
73 3
        return $writer->getData();
74
    }
75
76
    /**
77
     * Dump this file to a stream-wrapped target
78
     *
79
     * @param string $target Stream-wrapped target
80
     * @param array $parameters Writer parameters
81
     * @return WriterInterface Writer instance
82
     * @throws InvalidWriterArgumentException If an invalid reader stream wrapper is given
83
     */
84 3
    public function toTarget($target, ...$parameters)
85
    {
86 3
        $writer = Tools::writer($target, $parameters);
87 3
        if ($writer instanceof WriterInterface) {
88 2
            $this->dump($writer);
89 2
            return $writer;
90
        }
91
92 1
        throw new InvalidArgumentException(
93 1
            'Invalid writer stream wrapper',
94 1
            InvalidWriterArgumentException::INVALID_WRITER_STREAM_WRAPPER
95
        );
96
    }
97
98
    /**
99
     * Dump this files contents into a writer
100
     *
101
     * @param WriterInterface $writer Writer instance
102
     * @return Resource Self reference
103
     */
104
    abstract public function dump(WriterInterface $writer);
105
}
106