PHP Classes

File: Domain/ImgCountReport.php

Recommend this page to a friend!
  Classes of Igor Dyshlenko   PHP Image Crawler   Domain/ImgCountReport.php   Download  
File: Domain/ImgCountReport.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Image Crawler
Crawl Web site pages to find images in the pages
Author: By
Last change:
Date: 4 years ago
Size: 1,471 bytes
 

Contents

Class file image Download
<?php


namespace Domain;


use
Infrastructure\Repository\PageRepository;

class
ImgCountReport implements Report
{
   
/**
     * @var PageRepository
     */
   
protected $repository;

   
/**
     * @var string|null
     */
   
protected $html;

    public function
__construct(PageRepository $repository)
    {
       
$this->repository = $repository;
       
$repository->order(
            static function (
Page $a, Page $b) {
                if (
$a->getImgCount() === $b->getImgCount()) {
                    return
0;
                }
                return (
$a->getImgCount() > $b->getImgCount()) ? -1 : 1;
            }
        );
    }

   
/**
     * Get default report filename
     *
     * @return string
     */
   
public function getDefaultFilename(): string
   
{
        return
'report_' . date('d.m.Y') . '.html';
    }

    public function
getContent(): string
   
{
        if (
$this->html === null) {
           
$strings = '';
           
/** @var Page $page */
           
foreach ($this->repository->getPagesIterator() as $page) {
               
$strings .= "<tr>\n\t<td>{$page->getUrl()}</td>\n\t<td>{$page->getImgCount()}</td>\n\t<td>{$page->getProcessingTime()}</td>\n</tr>\n";
            }

           
$this->html =
               
"<table>\n<tr>\n\t<th>URL</th>\n\t<th>number of tags &lt;img&gt;</th>\n\t<th>processing time, sec.</th>\n</tr>\n" .
               
$strings . "</table>\n";
        }

        return
$this->html;
    }
}