From c4446eb3cbe8f7074d565ad9b68192b2c5d222f4 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 23 Apr 2021 20:53:49 +0200 Subject: Refactor Concert::create Restructure the method a bit, drop the `c`prefix on variables, rename the variable to hold the created concert, and use object notation to pass the attributes to the constructor. Also rename the method `get` to `find`, the only call site was the `create` method. Drop the unnecessary method `check_duplicate`. Just use `find` instead. --- includes/concert.php | 67 +++++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 40 deletions(-) (limited to 'includes/concert.php') diff --git a/includes/concert.php b/includes/concert.php index 22fc13d..aff41ac 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -51,41 +51,36 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } } - private static function check_duplicate($cname, $venue, $cdate, $ticketlink, $eventlink) + public static function create($name, $venue, $date, $ticketlink, $eventlink) { - global $wpdb; - - $cresults = GiglogAdmin_Concert::get($cname, $venue, $cdate); - if ($cresults) - return($cresults); - else - return ('new'); + if ( GiglogAdmin_Concert::find($name, $venue, $date) ) { + error_log( 'DUPLICATE ROW detected: ' + . ' CONCERT NAME ' . $name + . ', VENUE ID ' . $venue + . ', CONCERTDATE ' . $date); - } + return NULL; + } + else { + $concert = new GiglogAdmin_Concert( (object) [ + "wpgconcert_name" => $name, + "venue" => $venue, + "wpgconcert_date" => $date, + "wpgconcert_tickets" => $ticketlink, + "wpgconcert_event" => $eventlink, + ]); - public static function create($cname, $venue, $cdate, $ticketlink, $eventlink) - { - $c = GiglogAdmin_Concert::check_duplicate($cname, $venue, $cdate, $ticketlink, $eventlink); - if ($c=='new') - { - $attrs = new stdClass(); - $attrs->id = ''; - $attrs->wpgconcert_name = $cname; - $attrs->venue = $venue; - $attrs->wpgconcert_date = $cdate; - $attrs->wpgconcert_tickets = $ticketlink; - $attrs->wpgconcert_event = $eventlink; - $cid = new GiglogAdmin_Concert($attrs); - $cid->save(); + $concert->save(); error_log( 'NEW CONCERT ADDED: ' - . ' ID: ' . $cid -> id() - . ' CONCERT NAME ' . $cname - . ', VENUE ID ' . $venue - . ', CONCERTDATE ' . $cdate - . ', Ticket LINK ' . $ticketlink - . ', Event LINK ' . $eventlink); - GiglogAdmin_Concertlogs::add($cid->id()); + . ' ID: ' . $concert -> id() + . ' CONCERT NAME ' . $name + . ', VENUE ID ' . $venue + . ', CONCERTDATE ' . $date + . ', Ticket LINK ' . $ticketlink + . ', Event LINK ' . $eventlink); + + GiglogAdmin_Concertlogs::add( $concert->id() ); /*the last line can be replaced by a trigger CREATE TRIGGER `insertIntoPhotoLogs` AFTER INSERT ON `wpg_concerts` FOR EACH ROW INSERT INTO wpg_concertlogs ( @@ -96,15 +91,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { VALUES (null, new.id, 1) */ - return $cid; - } - else - { - error_log( 'DUPLICATE ROW detected: ' - . ' CONCERT NAME ' . $cname - . ', VENUE ID ' . $venue - . ', CONCERTDATE ' . $cdate); - return NULL; + return $concert; } } @@ -155,7 +142,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } - public static function get($cname, $venue, $date) + public static function find($cname, $venue, $date) { global $wpdb; -- cgit v1.2.3