Skip to content

Commit

Permalink
(refactor): processed feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike van den Hoek committed Jul 12, 2024
1 parent abc337d commit 3220af4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
26 changes: 26 additions & 0 deletions src/Leges/Repositories/LegesRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace OWC\PDC\Leges\Repositories;

use OWC\PDC\Leges\RestAPI\Repositories\LegesRepository as LegesRepositoryAPI;
use WP_Query;

class LegesRepository extends LegesRepositoryAPI
{
/**
* Posttype definition.
*/
protected string $posttype = 'pdc-leges';

public function all(): array
{
$args = array_merge($this->queryArgs, [
'post_type' => [$this->posttype],
'post_status' => 'publish',
]);

$this->query = new WP_Query($args);

return $this->getQuery()->posts;
}
}
10 changes: 5 additions & 5 deletions src/Leges/RestAPI/Controllers/LegesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function getLeges(WP_REST_Request $request): WP_REST_Response
*/
public function getLege(WP_REST_Request $request)
{
$id = $request->get_param('id');
$id = $this->handleRequestParam($request, 'id', 'int', 0);

if (empty($id) || ! is_numeric($id)) {
if (empty($id)) {
return new WP_Error('bad_request', 'Invalid ID', [
'status' => 400,
]);
Expand All @@ -59,9 +59,9 @@ public function getLege(WP_REST_Request $request)
*/
public function getLegeBySlug(WP_REST_Request $request)
{
$slug = $request->get_param('slug');
$slug = $this->handleRequestParam($request, 'slug', 'string', '');

if (empty($slug) || ! is_string($slug)) {
if (strlen($slug) === 0) {
return new WP_Error('bad_request', 'Invalid slug', [
'status' => 400,
]);
Expand All @@ -85,7 +85,7 @@ protected function addPaginator(array $data, WP_Query $query): array
{
$perPage = $query->get('posts_per_page');
$page = $query->get('paged');
$page = 0 == $page || -1 == $perPage ? 1 : $page; // If $perPage = -1, $page should be 1.
$page = $page = (0 == $page || -1 == $perPage) ? 1 : $page; // If $perPage = -1, $page should be 1.

return array_merge([
'data' => $data,
Expand Down
2 changes: 1 addition & 1 deletion src/Leges/RestAPI/Repositories/LegesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class LegesRepository extends AbstractRepository
{
/**
* Posttype definition
* Posttype definition.
*/
protected string $posttype = 'pdc-leges';

Expand Down
8 changes: 4 additions & 4 deletions src/Leges/WPCron/Events/UpdateLegesPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace OWC\PDC\Leges\WPCron\Events;

use DateTime;
use OWC\PDC\Leges\Repositories\LegesRepository;
use OWC\PDC\Leges\WPCron\Contracts\AbstractEvent;
use WP_Post;
use WP_Query;

class UpdateLegesPrices extends AbstractEvent
{
Expand Down Expand Up @@ -36,8 +36,8 @@ protected function execute(): void
*/
protected function getLeges(): array
{
$query = new WP_Query([
'post_type' => 'pdc-leges',
$repository = new LegesRepository();
$repository->addQueryArguments([
'posts_per_page' => -1,
'meta_query' => [
'relation' => 'AND',
Expand All @@ -54,7 +54,7 @@ protected function getLeges(): array
],
]);

return $query->posts ?: [];
return $repository->all();
}

/**
Expand Down

0 comments on commit 3220af4

Please sign in to comment.