summaryrefslogtreecommitdiffstats
path: root/library/SSRS/Report/CachedStreamResource.php
blob: 949c6d7be18b200da6e5ae1c348b709583fe67ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

namespace SSRS\Report;

use \SSRS\Object\RenderStream;

class CachedStreamResource {

    public $streamId;
    public $filePath;

    public function __construct($streamId, $filePath) {
        $this->streamId = $streamId;
        $this->filePath = $filePath;
    }

    public function read() {
        if (false === file_exists($this->filePath)) {
            throw new HttpUserException('Resource file not found', 404);
        }

        $data = file_get_contents($this->filePath);
        $parts = explode("\n", $data, 2);

        $stream = new RenderStream();
        $stream->MimeType = $parts[0];
        $stream->Result = $parts[1];

        return $stream;
    }

    public function store(RenderStream $renderStream) {
        $data = trim($renderStream->MimeType) . "\n";
        $data .= $renderStream->Result;

        file_put_contents($this->filePath, $data);
        return $this;
    }

    public function send() {
        $stream = $this->read();
        $stream->send();
        return $this;
    }

}