|
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) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
$start = $start + $limit; |
|
98
|
|
|
} while (count($result['result']) < $limit); |
|
99
|
|
|
} else { |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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.