Task: Create an Example of Code That Need Refactoring

Before

class CorelogicAu
{
    public function getPropertyDetail($id)
    {
        $response = $this->http->get(SELF::ENDPOINT_ADDRESS . $id);

        return $response->getBody()->toArray();
    }
}

class CorelogicNz
{
    public function getProperty(string $addressId)
    {
        $coreAddress = $this->http->get(SELF::ENDPOINT_ADDRESS . $id);
        $geoLocation = $this->http->get(SELF::ENDPOINT_ADDRESS_COORDINATE . $id);

        return array_merge(
            $coreAddress->getBody()->toArray(),
            $geoLocation->getBody()->toArray()
        );
    }
}

After

interface CorelogicDatasource
{
    public function getProperty(string $id): ?CorelogicProperty;
}

class CorelogicProperty
{
    public string $id;
    public string $address;
    public float $area;
    public float $latitude;
    public float $longitude;

    public function __construct(
        string $id,
        string $address,
        float $area,
        float $latitude,
        float $longitude
    )
    {
        $this->id = $id;
        $this->address = $address;
        $this->area = $area;
        $this->latitude = $latitude;
        $this->longitude = $longitude;
    }
}

class CorelogicAu implements CorelogicDatasource
{
    public function getProperty(string $id): ?CorelogicProperty
    {
        $response = $this->http->get(SELF::ENDPOINT_ADDRESS . $id);
        $response = $response->getBody()->json();

        if (empty($response->id)) {
            return null;
        }

        return new CorelogicProperty(
            $response->id,
            $response->address,
            $response->area,
            $response->latitude,
            $response->longitude,
        )
    }
}

class CorelogicNz implements CorelogicDatasource
{
    public function getProperty(string $id): ?CorelogicProperty
    {
        $coreAddress = $this->http->get(SELF::ENDPOINT_ADDRESS . $id);
        $coreAddress = $coreAddress->getBody()->json();

        $geoLocation = $this->http->get(SELF::ENDPOINT_ADDRESS_COORDINATE . $id);
        $geoLocation = $geoLocation->getBody()->json();

        if (empty($coreAddress->id)) {
            return null;
        }

        return new CorelogicProperty(
            $coreAddress->id,
            $coreAddress->address,
            $coreAddress->area,
            $geoLocation->latitude,
            $geoLocation->longitude,
        )
    }
}

28 July 2023

It’s written on my smartphone

We’re mostly doing social media activities on mobile phones.

Can we blog conveniently using smartphone?

I think so. I’m writing this using iA Writer and Markdown. And it’s fun!

You definitely need to give it a go.

Go ahead to Blot and you’ll know how it works.

7 July 2023 blogging

Threads hype

I got my Threads account yesterday. It was the same day Meta launched the platform. I was the 2 millionth person registering an account there.

This is the exact number: 2,217,033. It’s shown on my Instagram profile as a badge.

I think it makes sense for Meta to show this badge. It stimulates people’s FOMO to get involved in the new platform. And it works.

Many people said that it’s gonna beat Twitter. Let’s see. I’m not too into social media, but this sort of competition between tech giants becomes more interesting recently.

7 July 2023 journal social media Threads tweet

Doing a MySQL query to JSON fields case-insensitively using Laravel

If you’re stuck and wondering if there’s a way to query for a JSON metadata” in your database case-insensitively, there you go!

public function scopePropertyLike(Builder $query, string $keyword)
{
    $query->whereRaw(
        'LOWER(documents.metadata->"$.property.fullAddress") like ?',
        '%'. strtolower($keyword) . '%'
    );
}

As you see, you need to use a raw query to make it works. Please be aware of SQL injection whenever you will use raw queries. Make sure you always write them using variable bindings.

Alright. If you’re now wondering what the heck is $ there, it’s a part of a JSONPath notation. So documents is the table, metadata is the column, and "$.property.fullAddress" is the JSONPath expression of the data to query.

In this case, this is how the data looks like.

{
  "property": {
    "id": 12345,
    "fullAddress": "Jalan Malioboro, Yogyakarta",
    "province": "Daerah Istimewa Yogyakarta"
  }
}

You may want to find more references about JSONPath on JSONPath.com.

Hope it helps.

13 June 2023 coding Laravel

Treating people

People are unique. Every single person has a unique personality, including me and you. Sometimes people can’t understand me as well as I can’t understand them well.

Treating people isn’t easy. Sometimes they’re unpredictable, and sometimes they have a level of sensitivity multiple times beyond us. Learning to treat people requires a long run. With your partner, it’s lifelong learning.

5 December 2022 tweet

Dark jokes are dangerous

Yes, they are. That’s why we call them dark. Makes sense?

Having a broken heart is sad. Sometimes the feeling hurts so much like it can’t be handled anymore. For most people, it’s only a normal feeling. It’s getting better afterward.

But we must be aware that for some people, having a broken heart makes them fall into a horrible problem. Some people commit suicide because of a broken heart. It’s not funny at all.

These times dark jokes are almost everywhere. Let’s say a meme implicitly says, Oh hey, you’re rejected by your crush every time. Pity you. Come on, the world is even fine without you.”.

Most people aren’t facing terrible conditions, so they might be okay. How about the rest of the people? How about the last circumstances? Is there any bad news that makes the joke isn’t a joke?

Do some research. Talk to others, and get enough insights from them. If it’s sensitive, if it’s possible to make things worse, it must be avoided.

On regular days, we think that dark jokes are stupid, and we don’t even will follow them as in our reality. But do you remember that you were doing stupid things as the last choice to fix something?

Didn’t you smack your phone when it took a long time to connect to the internet? Didn’t you punch your mouse or keyboard when your computer was freezing? Didn’t you?

So please keep every joke in its place. Don’t try to put it everywhere. Otherwise, it will be dangerous.

22 October 2022 tweet

Refusing to give

I heard someone knocking my door when I was having a date with my laptop, writing codes as usual, in the working hours.

Wait, not only someone, it was more than just one person. It was different. Normal guests typically wouldn’t knock someone’s door aggressively.

You know what? I could guess that, they were beggars. And they were! 4 beggar women.

Being one of the most generous country, that kind of way of begging is an ironic picture in Indonesia.

There is a couple of investigations that uncover being beggar is a job, making a lot of money by seeking people’s sympathy.

I’m not trying to say that every beggar is begging as a job. We just need to better understand when to give money to beggars.

The beggars I mentioned above, they have come several times to my house. I decided to stop giving after their third return.

You know, they come again this morning. It had been so many times. All of them had been looking healthy. The way they walked, the way they shouted at my house. No sign that they had to beg. What the…!

Giving to every person isn’t going to make us human, instead it makes us robots. Give along with your heart. Give to people that really need.

Have a good day everyone!

19 October 2022 journal

I love rain

I love rain, I love petrichor. I love the atmosphere.

Rain is so precious, even in Islam there’s a special prayer to ask for rain, as well as another prayer when it rains.

Never blame rain, make peace with it. Believe me, when it doesn’t come in a while, you’ll miss it.

18 October 2022 tweet

Slam a glass

You know, an appreciation exactly after depreciation is rubbish.

You slam a glass down, lift that broken glass up, and say it’s okay.

7 July 2022 tweet

Family vs anybody else

Somebody who doesn’t belong to your family could be a really caring person more than somebody who belongs to your family.

So, be kind to anybody. Somebody who belongs to your family can always have a probability to have a conflict of interest.

22 June 2022 tweet