Code Duplication    Length = 34-34 lines in 3 locations

class/ExtcalPersistableObjectHandler.php 3 locations

@@ 247-280 (lines=34) @@
244
     *
245
     * @return int|array count of objects
246
     */
247
    public function getCount(CriteriaElement $criteria = null)
248
    {
249
        $field   = '';
250
        $groupby = false;
251
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
252
            if ('' != $criteria->groupby) {
253
                $groupby = true;
254
                $field   = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used
255
            }
256
        }
257
        $sql = 'SELECT ' . $field . 'COUNT(*) FROM ' . $this->table;
258
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
259
            $sql .= ' ' . $criteria->renderWhere();
260
            if ('' != $criteria->groupby) {
261
                $sql .= $criteria->getGroupby();
262
            }
263
        }
264
        $result = $this->db->query($sql);
265
        if (!$result) {
266
            return 0;
267
        }
268
        if (false === $groupby) {
269
            list($count) = $this->db->fetchRow($result);
270
271
            return $count;
272
        } else {
273
            $ret = [];
274
            while (list($id, $count) = $this->db->fetchRow($result)) {
275
                $ret[$id] = $count;
276
            }
277
278
            return $ret;
279
        }
280
    }
281
282
    /**
283
     * delete an object from the database by id.
@@ 625-658 (lines=34) @@
622
     *
623
     * @return array|string
624
     */
625
    public function getSum(CriteriaElement $criteria = null, $sum = '*')
626
    {
627
        $field   = '';
628
        $groupby = false;
629
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
630
            if ('' != $criteria->groupby) {
631
                $groupby = true;
632
                $field   = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used
633
            }
634
        }
635
        $sql = 'SELECT ' . $field . "SUM($sum) FROM " . $this->table;
636
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
637
            $sql .= ' ' . $criteria->renderWhere();
638
            if ('' != $criteria->groupby) {
639
                $sql .= $criteria->getGroupby();
640
            }
641
        }
642
        $result = $this->db->query($sql);
643
        if (!$result) {
644
            return 0;
645
        }
646
        if (false === $groupby) {
647
            list($sum) = $this->db->fetchRow($result);
648
649
            return $sum;
650
        } else {
651
            $ret = [];
652
            while (list($id, $sum) = $this->db->fetchRow($result)) {
653
                $ret[$id] = $sum;
654
            }
655
656
            return $ret;
657
        }
658
    }
659
660
    /**
661
     * @param null|CriteriaElement $criteria
@@ 666-699 (lines=34) @@
663
     *
664
     * @return array|string
665
     */
666
    public function getMax(CriteriaElement $criteria = null, $max = '*')
667
    {
668
        $field   = '';
669
        $groupby = false;
670
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
671
            if ('' != $criteria->groupby) {
672
                $groupby = true;
673
                $field   = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used
674
            }
675
        }
676
        $sql = 'SELECT ' . $field . "MAX($max) FROM " . $this->table;
677
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
678
            $sql .= ' ' . $criteria->renderWhere();
679
            if ('' != $criteria->groupby) {
680
                $sql .= $criteria->getGroupby();
681
            }
682
        }
683
        $result = $this->db->query($sql);
684
        if (!$result) {
685
            return 0;
686
        }
687
        if (false === $groupby) {
688
            list($max) = $this->db->fetchRow($result);
689
690
            return $max;
691
        } else {
692
            $ret = [];
693
            while (list($id, $max) = $this->db->fetchRow($result)) {
694
                $ret[$id] = $max;
695
            }
696
697
            return $ret;
698
        }
699
    }
700
701
    /**
702
     * @param null   $criteria