Skip to content

Commit

Permalink
models documentating and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tustin committed Dec 13, 2022
1 parent 19c3ec8 commit 5d9a357
Show file tree
Hide file tree
Showing 21 changed files with 182 additions and 274 deletions.
11 changes: 4 additions & 7 deletions src/Http/ResponseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@

use GuzzleHttp\Psr7\Response;

class ResponseParser {

class ResponseParser
{
/**
* Parses a GuzzleHttp Response.
*
* Will return one of three types of values:
* 1. JSON - will attempt to parse the response as JSON first.
* 2. String - if JSON was invalid and there is a response body, it will just return it as a string.
* 3. Response - if the JSON failed and the response body was empty, the entire Response object will be returned.
*
* @param Response $response
* @return mixed
*/
public static function parse(Response $response): mixed
{
$contents = $response->getBody()->getContents();

$data = json_decode($contents);

return (json_last_error() === JSON_ERROR_NONE) ? $data : (empty($contents) ? $response : $contents);
}
}
}
3 changes: 2 additions & 1 deletion src/Interfaces/FactoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace Tustin\PlayStation\Interfaces;

interface FactoryInterface
{
//
}
}
41 changes: 10 additions & 31 deletions src/Model/GameTitle.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Tustin\PlayStation\Model;

use Tustin\PlayStation\Model;
Expand All @@ -12,7 +13,10 @@ public function __construct(GameListFactory $gameListFactory, private string $id
parent::__construct($gameListFactory->getHttpClient());
}

public static function fromObject(GameListFactory $gameListFactory, object $data): GameTitle
/**
* Creates a new game title from existing data.
*/
public static function fromObject(GameListFactory $gameListFactory, object $data): self
{
$game = new static($gameListFactory, $data->titleId);
$game->setCache($data);
Expand All @@ -22,8 +26,6 @@ public static function fromObject(GameListFactory $gameListFactory, object $data

/**
* Gets the store concept for the game.
*
* @return Concept
*/
public function concept(): Concept
{
Expand All @@ -33,19 +35,15 @@ public function concept(): Concept
/**
* Gets the play duration.
* Example: "PT1192H44M48S"
*
* @return string
*/
public function playDuration(): string
{
return $this->pluck('playDuration');
}

/**
* Gets the first played date.
* Example: "2015-11-13T13:05:52Z"
*
* @return string
*/
public function firstPlayedDateTime(): string
{
Expand All @@ -55,8 +53,6 @@ public function firstPlayedDateTime(): string
/**
* Gets the last played date.
* Example: "2021-02-16T21:39:53.890Z"
*
* @return string
*/
public function lastPlayedDateTime(): string
{
Expand All @@ -65,8 +61,6 @@ public function lastPlayedDateTime(): string

/**
* Gets the last play count.
*
* @return int
*/
public function playCount(): int
{
Expand All @@ -76,8 +70,6 @@ public function playCount(): int
/**
* Gets the category.
* Example: "ps4_game", "ps4_nongame_mini_app", "ps5_native_game", "unknown"
*
* @return string
*/
public function category(): string
{
Expand All @@ -86,8 +78,6 @@ public function category(): string

/**
* Gets the localized image url.
*
* @return string
*/
public function localizedImageUrl(): string
{
Expand All @@ -96,8 +86,6 @@ public function localizedImageUrl(): string

/**
* Gets the image url.
*
* @return string
*/
public function imageUrl(): string
{
Expand All @@ -106,8 +94,6 @@ public function imageUrl(): string

/**
* Gets the localized name.
*
* @return string
*/
public function localizedName(): string
{
Expand All @@ -116,8 +102,6 @@ public function localizedName(): string

/**
* Gets the name.
*
* @return string
*/
public function name(): string
{
Expand All @@ -127,8 +111,6 @@ public function name(): string
/**
* Gets the id.
* Example: "CUSA02818_00"
*
* @return string
*/
public function id(): string
{
Expand All @@ -138,8 +120,6 @@ public function id(): string
/**
* Gets the service.
* Example: "none(purchased)", "none_purchased", "other"
*
* @return string
*/
public function service(): string
{
Expand All @@ -148,8 +128,6 @@ public function service(): string

/**
* Gets the stats.
*
* @return object
*/
public function stats(): object
{
Expand All @@ -158,14 +136,15 @@ public function stats(): object

/**
* Gets the media.
*
* @return object
*/
public function media(): object
{
return $this->pluck('media');
}


/**
* Gets the game title data.
*/
public function fetch(): object
{
return $this->get('gamelist/v2/users/' . $this->getFactory()->getUser()->accountId() . '/titles/' . $this->id());
Expand Down
5 changes: 4 additions & 1 deletion src/Model/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public function __construct(GroupsFactory $groupsFactory, private string $groupI
parent::__construct($groupsFactory->getHttpClient());
}

public static function fromObject(GroupsFactory $groupsFactory, object $data)
/**
* Creates a new group from existing data.
*/
public static function fromObject(GroupsFactory $groupsFactory, object $data): self
{
$instance = new static($groupsFactory, $data->groupId);
$instance->setCache($data);
Expand Down
5 changes: 4 additions & 1 deletion src/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public function __construct(Client $client, private string $ugcId)
parent::__construct($client);
}

public static function fromObject(Client $client, object $data): Media
/**
* Creates a new Media object from existing data.
*/
public static function fromObject(Client $client, object $data): self
{
$media = new static($client, $data->sourceUgcId);
$media->setCache($data);
Expand Down
20 changes: 2 additions & 18 deletions src/Model/Message/AbstractMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class AbstractMessage extends Model
*/
private MessageThread $thread;

public static function fromObject(MessageThread $thread, object $messageData)
public static function fromObject(MessageThread $thread, object $messageData): self
{
$instance = new static($thread->getHttpClient());
$instance->setCache($messageData);
Expand All @@ -27,15 +27,11 @@ public static function fromObject(MessageThread $thread, object $messageData)

/**
* Gets the message type.
*
* @return MessageType
*/
public abstract function type(): MessageType;

/**
* Gets the message id.
*
* @return string
*/
public function id(): string
{
Expand All @@ -44,8 +40,6 @@ public function id(): string

/**
* Gets the body of the message.
*
* @return string
*/
public function body(): string
{
Expand All @@ -54,18 +48,14 @@ public function body(): string

/**
* Gets the date and time when the message was posted.
*
* @return Carbon
*/
public function date(): Carbon
public function date(): \DateTime
{
return Carbon::parse($this->pluck('createdTimestamp'))->setTimezone('UTC');
}

/**
* Returns the message thread that this message is in.
*
* @return MessageThread
*/
public function messageThread(): MessageThread
{
Expand All @@ -74,8 +64,6 @@ public function messageThread(): MessageThread

/**
* Gets the message sender.
*
* @return User
*/
public function sender(): User
{
Expand All @@ -87,10 +75,6 @@ public function sender(): User

/**
* Creates a message based on the message type.
*
* @param MessageThread $thread
* @param object $messageData
* @return AbstractMessage
*/
public static function create(MessageThread $thread, object $messageData): AbstractMessage
{
Expand Down
5 changes: 4 additions & 1 deletion src/Model/Message/AudioMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

class AudioMessage extends AbstractMessage
{
/**
* Gets the message type.
*/
public function type(): MessageType
{
return MessageType::Audio;
}

public function fetch(): object
public function fetch(): object
{
throw new \BadMethodCallException();
}
Expand Down
7 changes: 3 additions & 4 deletions src/Model/Message/ImageMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class ImageMessage extends AbstractMessage
{
/**
* Gets the image media.
*
* @return Media
*/
public function image(): Media
{
Expand All @@ -20,14 +18,15 @@ public function image(): Media

/**
* Gets the resource id.
* @return string
*/
public function resourceId(): string
{
return $this->pluck('messageDetail.imageMessageDetail.resourceId');
}

/**
* Gets the message type.
*/
public function type(): MessageType
{
return MessageType::Image;
Expand Down
3 changes: 3 additions & 0 deletions src/Model/Message/Sendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

interface Sendable
{
/**
* Builds the message.
*/
public function build(): array;
}
15 changes: 15 additions & 0 deletions src/Model/Message/StickerMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,41 @@

class StickerMessage extends AbstractMessage
{
/**
* Gets the sticker url.
*/
public function url(): string
{
return $this->pluck('messageDetail.stickerMessageDetail.imageUrl');
}

/**
* Gets the sticker manifest url.
*/
public function manifestUrl(): string
{
return $this->pluck('messageDetail.stickerMessageDetail.manifestFileUrl');
}

/**
* Gets the sticker package id.
*/
public function packageId(): string
{
return $this->pluck('messageDetail.stickerMessageDetail.packageId');
}

/**
* Gets the sticker number.
*/
public function number(): string
{
return $this->pluck('messageDetail.stickerMessageDetail.number');
}

/**
* Gets the message type
*/
public function type(): MessageType
{
return MessageType::Sticker;
Expand Down
Loading

0 comments on commit 5d9a357

Please sign in to comment.