ExportController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A opml() 0 8 1
A articles() 0 20 3
1
<?php
2
/**
3
 * ownCloud - News
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @copyright Alessandro Cosentino 2012
11
 * @copyright Bernhard Posselt 2012, 2014
12
 */
13
14
namespace OCA\News\Controller;
15
16
use \OCP\IRequest;
17
use \OCP\AppFramework\Controller;
18
use \OCP\AppFramework\Http;
19
use \OCP\AppFramework\Http\JSONResponse;
20
21
use \OCA\News\Http\TextDownloadResponse;
22
use \OCA\News\Service\FolderService;
23
use \OCA\News\Service\FeedService;
24
use \OCA\News\Service\ItemService;
25
use \OCA\News\Utility\OPMLExporter;
26
27
class ExportController extends Controller {
28
29
    private $opmlExporter;
30
    private $folderService;
31
    private $feedService;
32
    private $itemService;
33
    private $userId;
34
35
    public function __construct($AppName,
36
                                IRequest $request,
37
                                FolderService $folderService,
38
                                FeedService $feedService,
39
                                ItemService $itemService,
40
                                OPMLExporter $opmlExporter,
41
                                $UserId){
42
        parent::__construct($AppName, $request);
43
        $this->feedService = $feedService;
44
        $this->folderService = $folderService;
45
        $this->opmlExporter = $opmlExporter;
46
        $this->itemService = $itemService;
47
        $this->userId = $UserId;
48
    }
49
50
51
    /**
52
     * @NoAdminRequired
53
     * @NoCSRFRequired
54
     */
55
    public function opml(){
56
        $feeds = $this->feedService->findAll($this->userId);
57
        $folders = $this->folderService->findAll($this->userId);
58
        $opml = $this->opmlExporter->build($folders, $feeds)->saveXML();
59
        $name = 'subscriptions.opml';
60
        $mimeType = 'text/xml';
61
        return new TextDownloadResponse($opml, $name, $mimeType);
62
    }
63
64
65
    /**
66
     * @NoAdminRequired
67
     * @NoCSRFRequired
68
     */
69
    public function articles(){
70
        $feeds = $this->feedService->findAll($this->userId);
71
        $items = $this->itemService->getUnreadOrStarred($this->userId);
72
73
        // build assoc array for fast access
74
        $feedsDict = [];
75
        foreach($feeds as $feed) {
76
            $feedsDict['feed' . $feed->getId()] = $feed;
77
        }
78
79
        $articles = [];
80
        foreach($items as $item) {
81
            $articles[] = $item->toExport($feedsDict);
82
        }
83
84
        $response = new JSONResponse($articles);
85
        $response->addHeader('Content-Disposition',
86
            'attachment; filename="articles.json"');
87
        return $response;
88
    }
89
90
91
}