Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render non markdown docs with github api still #89

Closed
wants to merge 4 commits into from

Conversation

mattstauffer
Copy link
Member

Seems like it actually works?!

image

@@ -111,13 +113,17 @@ public function getPreview()
return substr($body, 0, strpos($body, ' ', 200));
}

private function renderMarkdown()
private function renderMarkdown($content = null)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a cleaner way to decorate $this->content conditionally?

@@ -6,4 +6,10 @@ private function loadFixture($path)
{
return json_decode(file_get_contents(base_path() . '/tests/fixtures/gists/' . $path), true);
}

// Someone please help me name this
private function loadNonJsonFixture($path)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

help?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename the other one to loadJsonFixture and make this one just loadFixture.

@mattstauffer
Copy link
Member Author

Tag @JacobBennett

@JacobBennett
Copy link
Contributor

Looks like a fun challenge :) I'll take a look tonight. 

@@ -111,13 +112,17 @@ public function getPreview()
return substr($body, 0, strpos($body, ' ', 200));
}

private function renderMarkdown()
private function renderMarkdown($content = null)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use polymorphism, something like this...

<?php

$gistFactoryMap = [
    'Markdown' => function ($gist, $comments) {
        // Could also inject the Cache and ContentParser here and get rid of evil
        // facade usage.
        return new MarkdownGistlog(Gistlog::fromGitHub($gist, $comments));
    }
];

class GistlogRepository
{
    private $gistClient;
    private $gistFactoryMap;

    public function __construct(GistClient $gistClient, $gistFactoryMap)
    {
        $this->gistClient = $gistClient;
        $this->gistFactoryMap = $gistFactoryMap;
    }

    public function findById($id)
    {
        $gist = $this->gistClient->getGist($id);
        $comments = $this->gistClient->getGistComments($id);

        return $this->getGistBuilder($gist['files'][0]['type'])->__invoke($gist, $comments);
    }

    private function getGistBuilder($language)
    {
        // Default to the old one
        return array_get($this->gistFactoryMap, $language, function () {
            return function ($gist, $comments) {
                return Gistlog::fromGitHub($gist, $comments);
            };
        });
    }

    // ...and all those other methods, etc.
}

class MarkdownGistlog
{
    private $gistlog;

    public function __construct($gistlog)
    {
        $this->gistlog = $gistlog;
    }

    public function render()
    {
        if ($this->updatedAt == Cache::get('markdown.updated_at.' . $this->id)) {
            return Cache::get('markdown.' . $this->id);
        }
        $markdown = ContentParser::transform($this->content);
        Cache::forever('markdown.' . $this->id, $markdown);
        Cache::forever('markdown.updated_at.' . $this->id, $this->updatedAt);
        return $markdown;
    }

    public function __get($key)
    {
        return $this->gistlog->{$key};
    }
}

class Gistlog
{
    //... all the old stuff +

    public function render()
    {
        return "<pre><code>" . htmlspecialchars($this->content) . "\n</code></pre>";
    }
}

Would make it really easy to add other types down the line if other languages need to be rendered differently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah @adamwathan totally what I was thinking... took the words right out of my mouth... you just got to it before I did :) (this was sarcasm btw).

Also this has been up in my browser since you said "Replace Conditional with Polymorphism" at Laracon. Great use case!

After looking through this block of code you've written, so many cool things I wouldn't have thought to use. Mostly the use of closures: in the factory map and as a default for array_get.

I like the use of the decorator to change the render method. I would have reached for inheritance first. Is there a benefit to using the decorator pattern over inheritance in this instance or a rule of thumb to use when determining which is appropriate? @adamwathan @mattstauffer

@JacobBennett
Copy link
Contributor

@mattstauffer since you are working on gistlog today... 👍 any chance you still want this one taken care of?

@mattstauffer
Copy link
Member Author

@JacobBennett hahaha, I'm literally looking at this right now.

@mattstauffer
Copy link
Member Author

@JacobBennett is gonna take this one for HacktoberFest 2017. For some reason i can't assign him. ¯(°_o)/¯

@mattstauffer
Copy link
Member Author

... For some reason GitHub won't let me change the target branch! This is not really closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants