AlbumMapper   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 89.74%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 23
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 76
ccs 35
cts 39
cp 0.8974
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D buildAlbum() 0 48 22
1
<?php
2
3
namespace Mechpave\ImgurClient\Mapper;
4
5
use Mechpave\ImgurClient\Entity\AlbumInterface;
6
7
/**
8
 * Class AlbumMapper
9
 * @package Mechpave\ImgurClient\Mapper
10
 */
11
class AlbumMapper
12
{
13
    /**
14
     * @var ObjectMapper
15
     */
16
    protected $objectMapper;
17
18
    /**
19
     * @var ImageMapper
20
     */
21
    protected $imageMapper;
22
23
    /**
24
     * AlbumMapper constructor.
25
     * @param ObjectMapper $objectMapper
26
     */
27 2
    public function __construct(ObjectMapper $objectMapper, ImageMapper $imageMapper)
28
    {
29 2
        $this->objectMapper = $objectMapper;
30 2
        $this->imageMapper = $imageMapper;
31 2
    }
32
33
    /**
34
     * Build Album object from raw data
35
     * @param array $albumData
36
     * @return AlbumInterface
37
     */
38 2
    public function buildAlbum(array $albumData)
39
    {
40 2
        if (empty($albumData['id'])) {
41
            throw new \InvalidArgumentException();
42
        }
43 2
        $insertedIntoGallery = null;
44 2
        if (!empty($albumData['datetime'])) {
45 2
            $insertedIntoGallery = new \DateTime();
46 2
            $insertedIntoGallery->setTimestamp((int)$albumData['datetime']);
47
        }
48
49 2
        $albumClass = $this->objectMapper->getAlbumClass();
50
51
        /** @var AlbumInterface $album */
52 2
        $album = new $albumClass();
53
        $album
54 2
            ->setAlbumId($albumData['id'])
55 2
            ->setTitle(!empty($albumData['title']) ? $albumData['title'] : null)
56 2
            ->setDescription(!empty($albumData['description']) ? $albumData['description'] : null)
57 2
            ->setInsertedIntoGallery($insertedIntoGallery)
0 ignored issues
show
Bug introduced by
It seems like $insertedIntoGallery defined by null on line 43 can be null; however, Mechpave\ImgurClient\Ent...etInsertedIntoGallery() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58 2
            ->setCover(!empty($albumData['cover']) ? $albumData['cover'] : null)
59 2
            ->setCoverWidth(!empty($albumData['cover_width']) ? $albumData['cover_width'] : null)
60 2
            ->setCoverHeight(!empty($albumData['cover_height']) ? $albumData['cover_height'] : null)
61 2
            ->setAccountUsername(!empty($albumData['account_url']) ? $albumData['account_url'] : null)
62 2
            ->setAccountId(!empty($albumData['account_id']) ? $albumData['account_id'] : null)
63 2
            ->setPrivacy(!empty($albumData['privacy']) ? $albumData['privacy'] : null)
64 2
            ->setLayout(!empty($albumData['layout']) ? $albumData['layout'] : null)
65 2
            ->setViews(!empty($albumData['views']) ? $albumData['views'] : null)
66 2
            ->setLink(!empty($albumData['link']) ? $albumData['link'] : null)
67 2
            ->setFavorite(!empty($albumData['favorite']) ? $albumData['favorite'] : null)
68 2
            ->setNsfw(!empty($albumData['nsfw']) ? $albumData['nsfw'] : null)
69 2
            ->setSection(!empty($albumData['section']) ? $albumData['section'] : null)
70 2
            ->setOrder(!empty($albumData['order']) ? $albumData['order'] : null)
71 2
            ->setDeleteHash(!empty($albumData['deletehash']) ? $albumData['deletehash'] : null)
72 2
            ->setImagesCount(!empty($albumData['images_count']) ? $albumData['images_count'] : null);
73
74 2
        $images = [];
75 2
        if (!empty($albumData['images'])) {
76
            foreach ($albumData['images'] as $imageData) {
77
                $image = $this->imageMapper->buildImage($imageData);
78
                $images[] = $image;
79
            }
80
        }
81
82 2
        $album->setImages($images);
83
84 2
        return $album;
85
    }
86
}