-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from 5pm-HDH/feat/song-statistic
feat(song-statistic): add song-statistic
- Loading branch information
Showing
13 changed files
with
667 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
|
||
namespace CTApi\Models; | ||
|
||
|
||
use CTApi\Models\Traits\FillWithData; | ||
use CTApi\Requests\SongRequest; | ||
|
||
class SongStatistic | ||
{ | ||
use FillWithData; | ||
|
||
protected ?string $songId; | ||
protected array $dates = []; | ||
|
||
public static function createModelFromAjaxData(string $songId, array $dates): SongStatistic | ||
{ | ||
$statistics = new SongStatistic(); | ||
return $statistics->setSongId($songId) | ||
->setDates($dates); | ||
} | ||
|
||
public function requestSong(): ?Song | ||
{ | ||
if(!is_null($this->songId)){ | ||
return SongRequest::find((int) $this->songId); | ||
} | ||
return null; | ||
} | ||
|
||
public function getDates(): array | ||
{ | ||
return $this->dates; | ||
} | ||
|
||
public function getCount(): int | ||
{ | ||
return sizeof($this->getDates()); | ||
} | ||
|
||
public function getDatesForCalendars(array $calendarIds): array | ||
{ | ||
return array_values(array_filter($this->dates, function ($element) use ($calendarIds) { | ||
return in_array($element["calendar_id"], $calendarIds); | ||
})); | ||
} | ||
|
||
public function getCountForCalendars(array $calendarIds): int | ||
{ | ||
return sizeof($this->getDatesForCalendars($calendarIds)); | ||
} | ||
|
||
public function toData(): array | ||
{ | ||
$data = $this->convertPropertiesToData(); | ||
return array_merge( | ||
$data, | ||
['count' => $this->getCount()] | ||
); | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getSongId(): ?string | ||
{ | ||
return $this->songId; | ||
} | ||
|
||
/** | ||
* @param string|null $songId | ||
* @return SongStatistic | ||
*/ | ||
public function setSongId(?string $songId): SongStatistic | ||
{ | ||
$this->songId = $songId; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array $dates | ||
* @return SongStatistic | ||
*/ | ||
public function setDates(array $dates): SongStatistic | ||
{ | ||
$this->dates = array_map(function ($element) { | ||
return [ | ||
'date' => $element['date'] ?? null, | ||
'calendar_id' => $element['category_id'] ?? null | ||
]; | ||
}, $dates); | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
|
||
namespace CTApi\Requests; | ||
|
||
|
||
use CTApi\Models\Song; | ||
use CTApi\Models\SongStatistic; | ||
|
||
class SongStatisticRequest | ||
{ | ||
public static function all(): array | ||
{ | ||
return (new SongStatisticRequestBuilder())->all(); | ||
} | ||
|
||
public static function findOrFail(string $id): SongStatistic | ||
{ | ||
return (new SongStatisticRequestBuilder())->findOrFail($id); | ||
} | ||
|
||
public static function find(string $id): ?SongStatistic | ||
{ | ||
return (new SongStatisticRequestBuilder())->find($id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
|
||
namespace CTApi\Requests; | ||
|
||
|
||
use CTApi\Exceptions\CTRequestException; | ||
use CTApi\Models\SongStatistic; | ||
use CTApi\Requests\Traits\AjaxApi; | ||
use CTApi\Utils\CTResponseUtil; | ||
|
||
class SongStatisticRequestBuilder | ||
{ | ||
use AjaxApi; | ||
|
||
private array $data = []; | ||
|
||
/** | ||
* SongStatisticRequestBuilder constructor. | ||
* @param bool $isLazy determine if builder requests statistic-data for every request new | ||
*/ | ||
public function __construct( | ||
private bool $isLazy = true | ||
) | ||
{ | ||
} | ||
|
||
private function getStatisticData(): array | ||
{ | ||
if($this->isLazy && !empty($this->data)){ | ||
return $this->data; | ||
}else{ | ||
$response = $this->requestAjax("churchservice/ajax", "getSongStatistic", []); | ||
$this->data = CTResponseUtil::dataAsArray($response); | ||
return $this->data; | ||
} | ||
} | ||
|
||
public function findOrFail(string $songId) | ||
{ | ||
$model = $this->find($songId); | ||
if ($model != null) { | ||
return $model; | ||
} else { | ||
throw CTRequestException::ofModelNotFound(SongStatistic::class); | ||
} | ||
} | ||
|
||
public function find(string $songId): ?SongStatistic | ||
{ | ||
$data = $this->getStatisticData(); | ||
if(array_key_exists($songId, $data)){ | ||
return SongStatistic::createModelFromAjaxData($songId, $data[$songId]); | ||
} | ||
return null; | ||
} | ||
|
||
public function all(): array | ||
{ | ||
$data = $this->getStatisticData(); | ||
$modelArray = []; | ||
foreach($data as $songId => $dates) | ||
{ | ||
$modelArray[] = SongStatistic::createModelFromAjaxData($songId, $dates); | ||
} | ||
return $modelArray; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
|
||
namespace Tests\Integration\Requests; | ||
|
||
|
||
use CTApi\CTLog; | ||
use CTApi\Models\SongStatistic; | ||
use CTApi\Requests\SongRequest; | ||
use CTApi\Requests\SongStatisticRequest; | ||
use Tests\Integration\TestCaseAuthenticated; | ||
use Tests\Integration\TestData; | ||
|
||
class SongStatisticRequestTest extends TestCaseAuthenticated | ||
{ | ||
private int $songId; | ||
|
||
protected function setUp(): void | ||
{ | ||
if (!TestData::getValue("SONG_STATISTICS") == "YES") { | ||
$this->markTestSkipped("Test suite is disabled in testdata.ini"); | ||
} | ||
$this->songId = TestData::getValueAsInteger("SONG_STATISTICS_SONG_ID"); | ||
} | ||
|
||
public function testRequestAll() | ||
{ | ||
$data = SongStatisticRequest::all(); | ||
|
||
$this->assertNotEmpty($data); | ||
|
||
$lastSongStatisticElement = end($data); | ||
$this->assertNotEmpty($lastSongStatisticElement); | ||
$this->assertInstanceOf(SongStatistic::class, $lastSongStatisticElement); | ||
} | ||
|
||
public function testRequestSong() | ||
{ | ||
CTLog::enableHttpLog(); | ||
$song = SongRequest::findOrFail($this->songId); | ||
$songStatistic = $song->requestSongStatistic(); | ||
|
||
$this->assertNotNull($songStatistic); | ||
$this->assertEquals($songStatistic->getCount(), sizeof($songStatistic->getDates())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.