Skip to content

Commit e1bca49

Browse files
committed
Initial base commit for laravel web analyser application
0 parents  commit e1bca49

File tree

132 files changed

+9580
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+9580
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/application/vendor
2+
/node_modules
3+
/public/hot
4+
/public/storage
5+
/storage/*.key
6+
/vendor
7+
/.idea
8+
/.vscode
9+
/.vagrant
10+
Homestead.json
11+
Homestead.yaml
12+
npm-debug.log
13+
yarn-error.log
14+
.env
15+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Suraj Adsul
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

application/.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
* text=auto
2+
*.css linguist-vendored
3+
*.scss linguist-vendored
4+
*.js linguist-vendored
5+
CHANGELOG.md export-ignore

application/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/node_modules
2+
/public/hot
3+
/public/storage
4+
/storage/*.key
5+
/vendor
6+
/.idea
7+
/.vagrant
8+
Homestead.json
9+
Homestead.yaml
10+
npm-debug.log
11+
yarn-error.log
12+
.env
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Analyser;
4+
5+
use App\Checkers\Levels;
6+
use GuzzleHttp\Psr7\Uri;
7+
use Illuminate\Contracts\Container\Container;
8+
9+
class Checker
10+
{
11+
private $container;
12+
private $client;
13+
14+
public function __construct(Container $container, HttpClient $client)
15+
{
16+
$this->container = $container;
17+
$this->client = $client;
18+
}
19+
20+
/**
21+
* Extract info using different checkers.
22+
*
23+
* @param $url
24+
* @return \Generator
25+
*/
26+
public function check($url)
27+
{
28+
$url = new Uri($url);
29+
30+
$response = $this->client->fetchData($url);
31+
32+
if (! $this->client->isSuccessful()) {
33+
return yield [
34+
'passed' => false,
35+
'message' => $this->client->getStatusMessage(),
36+
];
37+
}
38+
39+
$crawler = new Crawler($response, $url);
40+
41+
foreach (config('analysers') as $analyserClassName) {
42+
$checker = $this->container->make($analyserClassName);
43+
44+
try {
45+
$result = $checker->check($crawler, $response, $url);
46+
yield [
47+
'passed' => $result,
48+
'message' => $result ? $checker->successMessage : $checker->failedMessage,
49+
'level' => $checker->level(),
50+
];
51+
} catch (\Exception $e) {
52+
yield [
53+
'passed' => false,
54+
'message' => "Error checking rule `{$analyserClassName}`.",
55+
'level' => Levels::ERROR,
56+
];
57+
}
58+
}
59+
}
60+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Analyser;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Symfony\Component\CssSelector\CssSelectorConverter;
7+
use Symfony\Component\DomCrawler\Crawler as BaseCrawler;
8+
9+
class Crawler extends BaseCrawler
10+
{
11+
protected $rawHtml;
12+
13+
/**
14+
* @param mixed $node A Node to use as the base for the crawling
15+
* @param string $currentUri The current URI
16+
* @param string $baseHref The base href value
17+
*/
18+
public function __construct($node = null, $currentUri = null, $baseHref = null)
19+
{
20+
if ($node instanceof ResponseInterface) {
21+
$node = (string) $node->getBody();
22+
}
23+
24+
parent::__construct($node, $currentUri, $baseHref);
25+
}
26+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace App\Analyser;
4+
5+
use GuzzleHttp\Client;
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
class HttpClient
9+
{
10+
/**
11+
* @var
12+
*/
13+
private $client;
14+
private $statusCode;
15+
16+
/**
17+
* HttpClient constructor.
18+
* @param Client|null $client
19+
*/
20+
public function __construct(Client $client = null)
21+
{
22+
$this->setClient($client ?: new Client());
23+
}
24+
25+
/**
26+
* @param Client $client
27+
*/
28+
private function setClient(Client $client)
29+
{
30+
$this->client = $client;
31+
}
32+
33+
/**
34+
* @param $url
35+
* @return $response
36+
*/
37+
public function fetchData($url)
38+
{
39+
$response = $this->client->get($url, ['exceptions' => false]);
40+
41+
$this->statusCode = $response->getStatusCode();
42+
43+
return $response;
44+
}
45+
46+
/**
47+
* @return bool
48+
*/
49+
public function isSuccessful(): bool
50+
{
51+
return $this->statusCode >= Response::HTTP_OK && $this->statusCode < Response::HTTP_MULTIPLE_CHOICES;
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getStatusMessage(): string
58+
{
59+
return $this->statusCode.': '.Response::$statusTexts[$this->statusCode];
60+
}
61+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Analyser;
4+
5+
use Parsedown;
6+
7+
class Markdown
8+
{
9+
/**
10+
* @var Parsedown
11+
*/
12+
private $parsedown;
13+
14+
/**
15+
* Markdown constructor.
16+
* @param Parsedown|null $parsedown
17+
*/
18+
public function __construct(Parsedown $parsedown = null)
19+
{
20+
$this->parsedown = $parsedown ?: new Parsedown();
21+
}
22+
23+
/**
24+
* Parse markdown into HTML.
25+
*
26+
* @param $text string The markdown text.
27+
*
28+
* @return string
29+
*/
30+
public function parse($text): string
31+
{
32+
return $this->parsedown->parse($text);
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Checkers;
4+
5+
use App\Analyser\Crawler;
6+
use App\Facades\Markdown;
7+
use Psr\Http\Message\UriInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
abstract class AbstractChecker implements CheckerInterface
11+
{
12+
abstract public function check(Crawler $crawler, ResponseInterface $response, UriInterface $uri);
13+
14+
public function level(): string
15+
{
16+
return Levels::WARNING;
17+
}
18+
19+
public function successMessage(): string
20+
{
21+
throw new \Exception('Method not implemented');
22+
}
23+
24+
public function failedMessage(): string
25+
{
26+
throw new \Exception('Method not implemented');
27+
}
28+
29+
public function __get($name): string
30+
{
31+
if (in_array($name, ['successMessage', 'failedMessage'], true)) {
32+
return Markdown::parse($this->$name());
33+
}
34+
35+
return $this->$name;
36+
}
37+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Checkers;
4+
5+
use App\Analyser\Crawler;
6+
use Psr\Http\Message\UriInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
interface CheckerInterface
10+
{
11+
/**
12+
* Extract info using checkers.
13+
*
14+
* @param Crawler $crawler
15+
* @param ResponseInterface $response
16+
* @param UriInterface $uri
17+
*
18+
* @return bool
19+
*/
20+
public function check(Crawler $crawler, ResponseInterface $response, UriInterface $uri);
21+
22+
/**
23+
* Get the critical level of the checker.
24+
*
25+
* @return string
26+
*/
27+
public function level();
28+
29+
/**
30+
* Get the message if the rule is passed.
31+
*
32+
* @return string
33+
*/
34+
public function successMessage();
35+
36+
/**
37+
* Get the message if the checker failed.
38+
*
39+
* @return string
40+
*/
41+
public function failedMessage();
42+
}

0 commit comments

Comments
 (0)