ImageMapper::buildImage()   D
last analyzed

Complexity

Conditions 10
Paths 2

Size

Total Lines 38
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10.0037

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 38
ccs 29
cts 30
cp 0.9667
rs 4.8197
cc 10
eloc 31
nc 2
nop 1
crap 10.0037

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mechpave\ImgurClient\Mapper;
4
5
use Mechpave\ImgurClient\Entity\ImageInterface;
6
7
/**
8
 * Class ImageMapper
9
 * @package Mechpave\ImgurClient\Mapper
10
 */
11
class ImageMapper
12
{
13
    /**
14
     * @var ObjectMapper
15
     */
16
    protected $objectMapper;
17
18
    /**
19
     * TokenMapper constructor.
20
     * @param ObjectMapper $objectMapper
21
     */
22 4
    public function __construct(ObjectMapper $objectMapper)
23
    {
24 4
        $this->objectMapper = $objectMapper;
25 4
    }
26
27
    /**
28
     * Builds Image object from raw data
29
     *
30
     * @param array $imageData
31
     * @return ImageInterface
32
     */
33 2
    public function buildImage(array $imageData)
34
    {
35 2
        if (empty($imageData['id'])) {
36
            throw new \InvalidArgumentException();
37
        }
38 2
        $uploadedAt = new \DateTime();
39 2
        $uploadedAt->setTimestamp((int)$imageData['datetime']);
40
41 2
        $imageClass = $this->objectMapper->getImageClass();
42
43
        /** @var ImageInterface $image */
44 2
        $image = new $imageClass();
45
        $image
46 2
            ->setImageId($imageData['id'])
47 2
            ->setTitle($imageData['title'])
48 2
            ->setDescription($imageData['description'])
49 2
            ->setUploadedAt($uploadedAt)
50 2
            ->setType($imageData['type'])
51 2
            ->setAnimated($imageData['animated'])
52 2
            ->setWidth($imageData['width'])
53 2
            ->setHeight($imageData['height'])
54 2
            ->setSize($imageData['size'])
55 2
            ->setViews($imageData['views'])
56 2
            ->setBandwith(!empty($imageData['bandwith']) ? $imageData['bandwith'] : null)
57 2
            ->setDeleteHash(!empty($imageData['deletehash']) ? $imageData['deletehash'] : null)
58 2
            ->setName(!empty($imageData['name']) ? $imageData['name'] : null)
59 2
            ->setSection(!empty($imageData['section']) ? $imageData['section'] : null)
60 2
            ->setLink($imageData['link'])
61 2
            ->setGifv(!empty($imageData['gifv']) ? $imageData['gifv'] : null)
62 2
            ->setMp4(!empty($imageData['mp4']) ? $imageData['mp4'] : null)
63 2
            ->setWebm(!empty($imageData['webm']) ? $imageData['webm'] : null)
64 2
            ->setLooping(!empty($imageData['looping']) ? $imageData['looping'] : null)
65 2
            ->setFavorite($imageData['favorite'])
66 2
            ->setNsfw($imageData['nsfw'])
67 2
            ->setVote($imageData['vote']);
68
69 2
        return $image;
70
    }
71
}