From 0e42a12a33b9bb193fa7851628edf018bd922362 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 18 Jun 2022 11:38:26 +0200 Subject: Add phpcs config and do automatic fixups. --- includes/concert.php | 254 ++++++++++++++++++++++++--------------------------- 1 file changed, 119 insertions(+), 135 deletions(-) (limited to 'includes/concert.php') diff --git a/includes/concert.php b/includes/concert.php index 162291d..a225a6e 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -7,18 +7,17 @@ require_once __DIR__ . '/venue.php'; -if ( !class_exists('GiglogAdmin_DuplicateConcertException') ) -{ - class GiglogAdmin_DuplicateConcertException extends Exception - { +if ( ! class_exists( 'GiglogAdmin_DuplicateConcertException' ) ) { + class GiglogAdmin_DuplicateConcertException extends Exception { + } } -if ( !class_exists('GiglogAdmin_Concert') ) { +if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { require_once __DIR__ . '/venue.php'; - class GiglogAdmin_Concert - { + class GiglogAdmin_Concert { + private ?int $id; private ?string $cname; private ?GiglogAdmin_Venue $venue; @@ -44,34 +43,31 @@ if ( !class_exists('GiglogAdmin_Concert') ) { * so this constructor can be used to construct the object * directly from the database row. */ - public function __construct(object $attrs) - { - $this->id = isset($attrs->id) ? $attrs->id : NULL; - $this->cname = isset($attrs->wpgconcert_name) ? $attrs->wpgconcert_name : NULL; - $this->cdate = isset($attrs->wpgconcert_date) ? $attrs->wpgconcert_date : NULL; - $this->tickets = isset($attrs->wpgconcert_tickets) ? $attrs->wpgconcert_tickets : NULL; - $this->eventlink = isset($attrs->wpgconcert_event) ? $attrs->wpgconcert_event : NULL; - $this->status = isset($attrs->wpgconcert_status) ? $attrs->wpgconcert_status : 0; - $this->roles = isset($attrs->wpgconcert_roles) ? json_decode($attrs->wpgconcert_roles, true) : []; - $this->created = isset($attrs->created) ? new DateTimeImmutable($attrs->created) : NULL; - $this->updated = isset($attrs->updated) ? new DateTimeImmutable($attrs->updated) : NULL; + public function __construct( object $attrs ) { + $this->id = isset( $attrs->id ) ? $attrs->id : null; + $this->cname = isset( $attrs->wpgconcert_name ) ? $attrs->wpgconcert_name : null; + $this->cdate = isset( $attrs->wpgconcert_date ) ? $attrs->wpgconcert_date : null; + $this->tickets = isset( $attrs->wpgconcert_tickets ) ? $attrs->wpgconcert_tickets : null; + $this->eventlink = isset( $attrs->wpgconcert_event ) ? $attrs->wpgconcert_event : null; + $this->status = isset( $attrs->wpgconcert_status ) ? $attrs->wpgconcert_status : 0; + $this->roles = isset( $attrs->wpgconcert_roles ) ? json_decode( $attrs->wpgconcert_roles, true ) : array(); + $this->created = isset( $attrs->created ) ? new DateTimeImmutable( $attrs->created ) : null; + $this->updated = isset( $attrs->updated ) ? new DateTimeImmutable( $attrs->updated ) : null; if ( isset( $attrs->venue ) ) { - if (isset($attrs->wpgvenue_name) && isset($attrs->wpgvenue_city)) { - $venue_attrs = (object) [ - "id" => $attrs->venue, - "wpgvenue_name" => $attrs->wpgvenue_name, - "wpgvenue_city" => $attrs->wpgvenue_city, - ]; - - $this->venue = new GiglogAdmin_Venue($venue_attrs); - } - else { - $this->venue = GiglogAdmin_Venue::get($attrs->venue); + if ( isset( $attrs->wpgvenue_name ) && isset( $attrs->wpgvenue_city ) ) { + $venue_attrs = (object) array( + 'id' => $attrs->venue, + 'wpgvenue_name' => $attrs->wpgvenue_name, + 'wpgvenue_city' => $attrs->wpgvenue_city, + ); + + $this->venue = new GiglogAdmin_Venue( $venue_attrs ); + } else { + $this->venue = GiglogAdmin_Venue::get( $attrs->venue ); } - } - else { - $this->venue = NULL; + } else { + $this->venue = null; } } @@ -81,31 +77,33 @@ if ( !class_exists('GiglogAdmin_Concert') ) { * @param int $id * @return null|self */ - static function get( int $id ) : ?self - { - return self::find_concerts(['id' => $id])[0]; + static function get( int $id ) : ?self { + return self::find_concerts( array( 'id' => $id ) )[0]; } - public static function create(string $name, int $venue_id, string $date, string $ticketlink, string $eventlink): ?self - { - $gigs = GiglogAdmin_Concert::find_concerts([ - 'name' => $name, - 'venue_id' => $venue_id, - 'date' => $date - ]); + public static function create( string $name, int $venue_id, string $date, string $ticketlink, string $eventlink ): ?self { + $gigs = self::find_concerts( + array( + 'name' => $name, + 'venue_id' => $venue_id, + 'date' => $date, + ) + ); - if (!empty($gigs)) { + if ( ! empty( $gigs ) ) { throw new GiglogAdmin_DuplicateConcertException( - "Duplicate concert: name: {$name}, venue_id: {$venue_id}, date: {$date}"); - } - else { - $concert = new GiglogAdmin_Concert( (object) [ - "wpgconcert_name" => $name, - "venue" => $venue_id, - "wpgconcert_date" => $date, - "wpgconcert_tickets" => $ticketlink, - "wpgconcert_event" => $eventlink, - ]); + "Duplicate concert: name: {$name}, venue_id: {$venue_id}, date: {$date}" + ); + } else { + $concert = new GiglogAdmin_Concert( + (object) array( + 'wpgconcert_name' => $name, + 'venue' => $venue_id, + 'wpgconcert_date' => $date, + 'wpgconcert_tickets' => $ticketlink, + 'wpgconcert_event' => $eventlink, + ) + ); $concert->save(); @@ -114,54 +112,52 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } - public function update(object $attrs) : bool - { + public function update( object $attrs ) : bool { $need_update = false; - if (isset($attrs->wpgconcert_name) && $attrs->wpgconcert_name != $this->cname) { + if ( isset( $attrs->wpgconcert_name ) && $attrs->wpgconcert_name != $this->cname ) { $this->cname = $attrs->wpgconcert_name; $need_update = true; } - if (isset($attrs->wpgconcert_date) && $attrs->wpgconcert_date != $this->cdate) { + if ( isset( $attrs->wpgconcert_date ) && $attrs->wpgconcert_date != $this->cdate ) { $this->cdate = $attrs->wpgconcert_date; $need_update = true; } - if (isset($attrs->wpgconcert_tickets) && $attrs->wpgconcert_tickets != $this->tickets) { + if ( isset( $attrs->wpgconcert_tickets ) && $attrs->wpgconcert_tickets != $this->tickets ) { $this->tickets = $attrs->wpgconcert_tickets; $need_update = true; } - if (isset($attrs->wpgconcert_event) && $attrs->wpgconcert_event != $this->eventlink) { + if ( isset( $attrs->wpgconcert_event ) && $attrs->wpgconcert_event != $this->eventlink ) { $this->eventlink = $attrs->wpgconcert_eventlink; $need_update = true; } - if (isset($attrs->wpgconcert_status) && $attrs->wpgconcert_status != $this->status) { + if ( isset( $attrs->wpgconcert_status ) && $attrs->wpgconcert_status != $this->status ) { $this->status = $attrs->wpgconcert_status; $need_update = true; } - if (isset($attrs->wpgconcert_roles) && $attrs->wpgconcert_roles != $this->roles) { + if ( isset( $attrs->wpgconcert_roles ) && $attrs->wpgconcert_roles != $this->roles ) { $this->roles = $attrs->wpgconcert_roles; $need_update = true; } - if (isset($attrs->venue) && $attrs->venue != $this->venue()->id()) { - $this->venue = GiglogAdmin_Venue::get($attrs->venue); + if ( isset( $attrs->venue ) && $attrs->venue != $this->venue()->id() ) { + $this->venue = GiglogAdmin_Venue::get( $attrs->venue ); $need_update = true; } - if ($need_update) { + if ( $need_update ) { $this->save(); } return $need_update; } - private static function _build_query(array $filter = [], bool $count = false) : string - { + private static function _build_query( array $filter = array(), bool $count = false ) : string { global $wpdb; $ct = "{$wpdb->prefix}giglogadmin_concerts"; @@ -169,63 +165,62 @@ if ( !class_exists('GiglogAdmin_Concert') ) { if ( $count ) { $query = "SELECT count({$ct}.id) "; - } - else { + } else { $query = "SELECT {$ct}.*, {$vt}.wpgvenue_name, {$vt}.wpgvenue_city "; } $query .= "FROM {$ct} LEFT JOIN {$vt} ON {$ct}.venue = {$vt}.id WHERE wpgconcert_date >= CURRENT_TIMESTAMP"; - $keymap = [ + $keymap = array( 'id' => $wpdb->prefix . 'giglogadmin_concerts.id', 'name' => 'wpgconcert_name', 'date' => 'wpgconcert_date', - 'month'=> 'MONTH(wpgconcert_date)', + 'month' => 'MONTH(wpgconcert_date)', 'venue_id' => $wpdb->prefix . 'giglogadmin_venues.id', 'venue' => $wpdb->prefix . 'giglogadmin_venues.wpgvenue_name', 'city' => $wpdb->prefix . 'giglogadmin_venues.wpgvenue_city', 'currentuser' => 'wpgconcert_roles', - ]; + ); - $where = []; - $lmt=[]; - foreach( $filter as $key => $value ) { - switch ($key) { + $where = array(); + $lmt = array(); + foreach ( $filter as $key => $value ) { + switch ( $key ) { case 'name': case 'date': case 'month': case 'venue': case 'city': - array_push($where, $wpdb->prepare($keymap[$key] . '=%s', $value)); + array_push( $where, $wpdb->prepare( $keymap[ $key ] . '=%s', $value ) ); break; case 'id': case 'venue_id': - array_push($where, $wpdb->prepare($keymap[$key] . '=%d', $value)); + array_push( $where, $wpdb->prepare( $keymap[ $key ] . '=%d', $value ) ); break; case 'currentuser': - array_push($where, $wpdb->prepare($keymap[$key] . ' like "%%%s%%"', $value)); + array_push( $where, $wpdb->prepare( $keymap[ $key ] . ' like "%%%s%%"', $value ) ); break; case 'offset': - array_push($lmt, $value); + array_push( $lmt, $value ); break; case 'recperpage': - array_push($lmt, $value); + array_push( $lmt, $value ); break; } } if ( ! empty( $where ) ) { - $query .= ' AND ' . implode(' and ', $where); + $query .= ' AND ' . implode( ' and ', $where ); } $query .= ' ORDER BY wpgconcert_date'; if ( ! empty( $lmt ) ) { - $query .= ' LIMIT ' . implode(', ', $lmt); + $query .= ' LIMIT ' . implode( ', ', $lmt ); } return $query; @@ -242,14 +237,18 @@ if ( !class_exists('GiglogAdmin_Concert') ) { * @param array $filter * @return array */ - public static function find_concerts(array $filter = []) : array - { + public static function find_concerts( array $filter = array() ) : array { global $wpdb; - $query = Self::_build_query($filter, false); - $results = $wpdb->get_results($query); + $query = self::_build_query( $filter, false ); + $results = $wpdb->get_results( $query ); - return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results); + return array_map( + function( $c ) { + return new GiglogAdmin_Concert( $c ); + }, + $results + ); } /** @@ -258,21 +257,19 @@ if ( !class_exists('GiglogAdmin_Concert') ) { * @param array $filter * @return int */ - public static function count(array $filter = []) : int - { + public static function count( array $filter = array() ) : int { global $wpdb; - $query = Self::_build_query($filter, true); - $count = $wpdb->get_var($query); + $query = self::_build_query( $filter, true ); + $count = $wpdb->get_var( $query ); return $count ? $count : 0; } - public function save() : void - { + public function save() : void { global $wpdb; - $columns = [ + $columns = array( 'wpgconcert_name' => $this->cname, 'venue' => $this->venue->id(), 'wpgconcert_date' => $this->cdate, @@ -280,57 +277,47 @@ if ( !class_exists('GiglogAdmin_Concert') ) { 'wpgconcert_event' => $this->eventlink, 'wpgconcert_status' => $this->status, 'wpgconcert_roles' => wp_json_encode( $this->roles ), - ]; + ); - if ( $this->id !== NULL ) { - $res = $wpdb->update( $wpdb->prefix . 'giglogadmin_concerts', $columns, [ 'id' => $this->id ] ); - } - else { - $res = $wpdb->insert( $wpdb->prefix . 'giglogadmin_concerts', $columns); + if ( $this->id !== null ) { + $res = $wpdb->update( $wpdb->prefix . 'giglogadmin_concerts', $columns, array( 'id' => $this->id ) ); + } else { + $res = $wpdb->insert( $wpdb->prefix . 'giglogadmin_concerts', $columns ); } if ( $res === false ) { $wpdb->print_error( __METHOD__ ); - } - elseif ( $this->id === NULL ) { + } elseif ( $this->id === null ) { $this->id = $wpdb->insert_id; } } - public function id() - { - return $this->id; + public function id() { + return $this->id; } - public function cname() - { - return $this->cname; + public function cname() { + return $this->cname; } - public function venue() - { - return $this->venue; + public function venue() { + return $this->venue; } - public function cdate() - { - return $this->cdate; + public function cdate() { + return $this->cdate; } - public function tickets() - { - return $this->tickets; + public function tickets() { + return $this->tickets; } - public function eventlink() - { - return $this->eventlink; + public function eventlink() { + return $this->eventlink; } - public function status() - { - return $this->status; + public function status() { + return $this->status; } - public function set_status( int $new_status ) - { - $this->status = $new_status; + public function set_status( int $new_status ) { + $this->status = $new_status; } /** @@ -338,19 +325,16 @@ if ( !class_exists('GiglogAdmin_Concert') ) { * * @return array */ - public function roles() : array - { + public function roles() : array { return $this->roles; } - public function assign_role( string $role, string $username ) : void - { - $this->roles[$role] = $username; + public function assign_role( string $role, string $username ) : void { + $this->roles[ $role ] = $username; } - public function remove_user_from_roles( string $username ) : void - { - $this->roles = array_filter($this->roles, fn($u) => $u != $username); + public function remove_user_from_roles( string $username ) : void { + $this->roles = array_filter( $this->roles, fn( $u) => $u != $username ); } public function created() : DateTimeImmutable { @@ -362,4 +346,4 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } } } -?> + -- cgit v1.2.3