Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SuccessCache
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 stashImportResult
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 fetchImportResult
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 makeCacheKey
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace FileImporter\Services;
4
5use MediaWiki\Title\Title;
6use MediaWiki\User\UserIdentity;
7use Psr\Log\LoggerInterface;
8use Psr\Log\NullLogger;
9use StatusValue;
10use Wikimedia\LightweightObjectStore\ExpirationAwareness;
11use Wikimedia\ObjectCache\BagOStuff;
12
13/**
14 * Save the import results to cache so that they can be looked up from the success page.
15 *
16 * @license GPL-2.0-or-later
17 */
18class SuccessCache {
19
20    private const CACHE_KEY = 'fileImporter_result';
21
22    private BagOStuff $cache;
23    private LoggerInterface $logger;
24
25    public function __construct( BagOStuff $cache, ?LoggerInterface $logger = null ) {
26        $this->cache = $cache;
27        $this->logger = $logger ?? new NullLogger();
28    }
29
30    /**
31     * @return bool If caching was successful or not.
32     */
33    public function stashImportResult( Title $targetTitle, UserIdentity $user, StatusValue $importResult ) {
34        $key = $this->makeCacheKey( $targetTitle, $user );
35        $this->logger->debug( __METHOD__ . ': Import result cached at ' . $key );
36        return $this->cache->set( $key, $importResult, ExpirationAwareness::TTL_DAY );
37    }
38
39    /**
40     * @return StatusValue|null
41     */
42    public function fetchImportResult( Title $targetTitle, UserIdentity $user ) {
43        $key = $this->makeCacheKey( $targetTitle, $user );
44        $importResult = $this->cache->get( $key );
45        if ( !( $importResult instanceof StatusValue ) ) {
46            $this->logger->error( __METHOD__ . ': Failed to retrieve import result from ' . $key );
47            return null;
48        }
49        return $importResult;
50    }
51
52    /**
53     * @return string
54     */
55    private function makeCacheKey( Title $targetTitle, UserIdentity $user ) {
56        return $this->cache->makeKey(
57            __CLASS__,
58            self::CACHE_KEY,
59            $targetTitle->getPrefixedDBkey(),
60            $user->getId()
61        );
62    }
63
64}