Code Duplication    Length = 30-30 lines in 2 locations

src/Xtools/EditCounterRepository.php 2 locations

@@ 408-437 (lines=30) @@
405
     * @param string $username The username.
406
     * @return string[]
407
     */
408
    public function getMonthCounts(Project $project, User $user)
409
    {
410
        $username = $user->getUsername();
411
        $cacheKey = "monthcounts.$username";
412
        if ($this->cache->hasItem($cacheKey)) {
413
            return $this->cache->getItem($cacheKey)->get();
414
        }
415
416
        $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision');
417
        $pageTable = $this->getTableName($project->getDatabaseName(), 'page');
418
        $sql =
419
            "SELECT " . "     YEAR(rev_timestamp) AS `year`," .
420
            "     MONTH(rev_timestamp) AS `month`," . "     page_namespace," .
421
            "     COUNT(rev_id) AS `count` "
422
            . " FROM $revisionTable    JOIN $pageTable ON (rev_page = page_id)" .
423
            " WHERE rev_user_text = :username" .
424
            " GROUP BY YEAR(rev_timestamp), MONTH(rev_timestamp), page_namespace " .
425
            " ORDER BY rev_timestamp DESC";
426
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
427
        $resultQuery->bindParam(":username", $username);
428
        $resultQuery->execute();
429
        $totals = $resultQuery->fetchAll();
430
        
431
        $cacheItem = $this->cache->getItem($cacheKey);
432
        $cacheItem->expiresAfter(new DateInterval('PT10M'));
433
        $cacheItem->set($totals);
434
        $this->cache->save($cacheItem);
435
436
        return $totals;
437
    }
438
439
    /**
440
     * Get yearly edit totals for this user, grouped by namespace.
@@ 444-473 (lines=30) @@
441
     * @param string $username
442
     * @return string[] ['<namespace>' => ['<year>' => 'total', ... ], ... ]
443
     */
444
    public function getYearCounts(Project $project, User $user)
445
    {
446
        $username = $user->getUsername();
447
        $cacheKey = "yearcounts.$username";
448
        if ($this->cache->hasItem($cacheKey)) {
449
            return $this->cache->getItem($cacheKey)->get();
450
        }
451
452
        $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision');
453
        $pageTable = $this->getTableName($project->getDatabaseName(), 'page');
454
        $sql = "SELECT "
455
            . "     SUBSTR(CAST(rev_timestamp AS CHAR(4)), 1, 4) AS `year`,"
456
            . "     page_namespace,"
457
            . "     COUNT(rev_id) AS `count` "
458
            . " FROM $revisionTable    JOIN $pageTable ON (rev_page = page_id)"
459
            . " WHERE rev_user_text = :username"
460
            . " GROUP BY SUBSTR(CAST(rev_timestamp AS CHAR(4)), 1, 4), page_namespace "
461
            . " ORDER BY rev_timestamp DESC ";
462
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
463
        $resultQuery->bindParam(":username", $username);
464
        $resultQuery->execute();
465
        $totals = $resultQuery->fetchAll();
466
467
        $cacheItem = $this->cache->getItem($cacheKey);
468
        $cacheItem->set($totals);
469
        $cacheItem->expiresAfter(new DateInterval('P10M'));
470
        $this->cache->save($cacheItem);
471
472
        return $totals;
473
    }
474
475
    /**
476
     * Get data for the timecard chart, with totals grouped by day and to the nearest two-hours.