Completed
Push — master ( 7a4c9e...09d75e )
by Gianluca
04:00
created

ImportCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 14.89 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 0
cbo 2
dl 14
loc 94
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C run() 14 91 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace App\Command;
3
4
use ZF\Console\Route;
5
use Zend\Console\Adapter\AdapterInterface;
6
use App\Service\WpPostService;
7
use App\Service\SingRingApiService;
8
9
class ImportCommand
10
{
11
    public static function run(Route $route, AdapterInterface $console, $container)
12
    {
13
        $singRingApi = $container->get(SingRingApiService::class);
14
        $wpPost = $container->get(WpPostService::class);
15
        $timestamp = $route->getMatchedParam("timestamp", null);
16
        $start = 0;
17
        $limit = 1000;
18
        $apiKey = "a125163ffd73ace7c03e338e8e48057a8ccf36d4";
19
        $apiId = "792038355642229";
20
        $albumDB = null;
21
        $artistDB = null;
22
        if ($timestamp == null) {
23
            do {
24
                $result = $singRingApi->getAllSongs($apiKey, $apiId, $start, $limit);
25
                foreach ($result->result as $song) {
26
                    $song = $singRingApi->getLyricById($apiKey, $apiId, $song->id);
27
                    if ($song->artist->name) {
28
                        $artistDB = $wpPost->findOneArtistByName($song->artist->name);
29
                        if ($artistDB == null) {
30
                            $artistDB = $wpPost->insertPost([
31
                                ':post_name' => str_replace(" ", "-", strtolower($song->artist->name)),
32
                                ':post_date' => date("Y-m-d H:i:s"),
33
                                ':post_gmt_date' => date("Y-m-d H:i:s"),
34
                                ':post_title' => $song->artist->name,
35
                                ':post_content' => "",
36
                                ':post_status' => 'publish',
37
                                ':comment_status' => 'closed',
38
                                ':ping_status' => 'closed',
39
                                ':post_type' => 'artist',
40
                            ]);
41
                            $console->writeLine("[INFO] New artist ({$artistDB['ID']}) {$artistDB['post_title']}");
42
                        }
43
                    }
44
                    if ($song->album->name) {
45
                        $albumDB = $wpPost->findOneAlbumByName($song->album->name);
46
                        if ($albumDB == null) {
47
                            $albumDB = $wpPost->insertPost([
48
                                ':post_name' => str_replace(" ", "-", strtolower($song->album->name)),
49
                                ':post_date' => date("Y-m-d H:i:s"),
50
                                ':post_gmt_date' => date("Y-m-d H:i:s"),
51
                                ':post_title' => $song->album->name,
52
                                ':post_content' => "",
53
                                ':post_status' => 'publish',
54
                                ':comment_status' => 'closed',
55
                                ':ping_status' => 'closed',
56
                                ':post_type' => 'album',
57
                            ]);
58
                            $wpPost->insertMeta([
59
                                ':post_id' => $albumDB['ID'],
60
                                ':key' => 'theme2035_album_artist_name',
61
                                ':value' => $artistDB['ID'],
62
                            ]);
63
                            $console->writeLine("[INFO] New album ({$albumDB['ID']}) {$albumDB['post_title']}");
64
                        }
65
                    }
66
                    $songDB = $wpPost->findOneLyricByTitle($song->title);
67
                    if ($songDB == null) {
68
                        $songDB = $wpPost->insertPost([
69
                            ':post_name' => str_replace(" ", "-", strtolower($song->title)),
70
                            ':post_date' => date("Y-m-d H:i:s"),
71
                            ':post_gmt_date' => date("Y-m-d H:i:s"),
72
                            ':post_title' => $song->title,
73
                            ':post_content' => str_replace('\n', "<\br>", $song->text),
74
                            ':post_status' => 'publish',
75
                            ':comment_status' => 'closed',
76
                            ':ping_status' => 'closed',
77
                            ':post_type' => 'lyrics',
78
                        ]);
79 View Code Duplication
                        if ($albumDB != null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
                            $wpPost->insertMeta([
81
                                ':post_id' => $songDB['ID'],
82
                                ':key' => 'theme2035_album_name',
83
                                ':value' => $albumDB['ID'],
84
                            ]);
85
                        }
86 View Code Duplication
                        if ($artistDB != null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
                            $wpPost->insertMeta([
88
                                ':post_id' => $songDB['ID'],
89
                                ':key' => 'theme2035_album_name',
90
                                ':value' => $artistDB['ID'],
91
                            ]);
92
                        }
93
                        $console->writeLine("[INFO] New lyrics ({$songDB['ID']}) {$songDB['post_title']}");
94
                    }
95
                        die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
96
                }
97
                $start = $start + $limit;
98
            } while (count($result['result']) < $limit);
99
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
100
        }
101
    }
102
}
103